c# - AccessText 类的 WPF 使用和预期结果

标签 c# wpf xaml data-binding

我今天正在上课 AccessText。我无法确定使用此类的确切用途和结果。

If you use AccessText with a Label and use Target property as a TextBox, the TextBox will get focus when access key is pressed.see below code:

<StackPanel>
    <Label Target="{Binding ElementName=txtbox}">
        <AccessText>_first_second</AccessText>
    </Label>
    <TextBox Name="txtbox" Width="100" Height="50"/>
</StackPanel>

If you use AccessText with a Label and use Target property as a Buton , the Button Click event will get executed when access key is pressed.

所以我的问题是

1.what is the definitive behavior of AccessText Class? If I have to predict it's uses with other type of controls like DataGrid, ComboBox, RadioButton? how can i be sure of expected result?

2.Why is this class derived from FrameworkElement? What applications does it have as a FrameworkElement? seems a bit more for just specifying Accesskeys etc.

最佳答案

AccessText 是一个 FrameworkElement,它的行为或多或少像一种特殊类型的 TextBlock,它允许任何键盘字符跟在单个下划线 (_) 作为访问 key 。

对于给定的控件,关联访问键的行为取决于其 OnAccessKey 方法。 OnAccessKeyUIElement的一个虚方法,提供如下定义:

protected virtual void OnAccessKey(AccessKeyEventArgs e)
{
    this.Focus();
}

因此,任何不覆盖 UIElement 定义的 OnAccessKey 定义的控件都将保持默认行为,即控件在访问键被按下。

ButtonBaseButton 继承自,OnAccessKey 定义如下:

protected override void OnAccessKey(AccessKeyEventArgs e)
{
    if (e.IsMultiple)
        base.OnAccessKey(e);
    else
        this.OnClick();
}

因此 Button 和其他继承自 ButtonBase 的控件的默认行为是,如果 IsMultiple 为真,则将控件置于焦点,否则,它将引发点击事件。 (如果访问键与多个 UIElement 关联,则 IsMultiple 为真。)

考虑到这一背景,以下是您具体问题的答案:

  1. 用作控件的 ContentPresenterAccessText 元素的最终行为是将单个下划线后的第一个字母注册到 AccessKeyManager,当按键被按下时,它将调用控件的 OnAccessKey 方法。要了解这对特定控件的作用,需要了解 OnAccessKey 的哪个定义对该控件有效。如果在其继承链中没有覆盖,按下访问键将使控件成为焦点。如果有覆盖,行为将取决于覆盖方法的定义。这可以通过实验、阅读相关文档或检查源代码来确定。

  2. AccessText 是一个 FrameworkElement,原因与 TextBlock 是一个 FrameworkElement 的原因相同。它具有视觉形式并占用布局系统在相对于它定位其他元素时需要考虑的空间。此外,FrameworkElements 允许样式化,并且它们拥有自己的 DataContext 属性,这允许绑定(bind)场景,否则是不可能的。如果 AccessText 不是 FrameworkElement,它将不必要地限制和阻止 WPF 开发人员可能拥有的合理(尽管可能很少)用例。

编辑

这是一个精美的电源按钮示例,它演示了 AccessText 作为 FrameworkElement 的有用性:

<Grid>
    <Button Width="150"
            Height="35"
            Command="{Binding PowerCommand}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Status" />
            <Rectangle Margin="5,2,0,0"
                       Width="10"
                       Height="10"
                       Fill="{Binding PowerFill}" />
            <AccessText Margin="25,0,0,0"
                        Text="{Binding PowerText}" />
        </StackPanel>
    </Button>
</Grid>

这导致(在按下 Alt 之后):

enter image description here

点击按钮或按下Alt+S后, View 模型将通过更改Text来响应命令>Fill,结果是:

enter image description here

再次单击或使用访问键将返回到第一个状态。

关于c# - AccessText 类的 WPF 使用和预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35534223/

相关文章:

c# - 如何为表单设置点击事件?

c# - 从 MM/dd/yy 格式创建有效日期

c# - 将枚举绑定(bind)到 DataGrid ComboBox View

WPF 绑定(bind)样式到另一个控件的属性

c# - 卸载控件时无法获取附加的行为属性值

c# - 文本 block 文本选择

c# - 如何将 DataContext 设置为多个类?

c# - 以下两个语句有什么区别?

c# - 如何使用 EF 和 WPF 显示和更新来自多个表的数据网格中的数据?

wpf - 在 WPF 控件之间添加空格