WPF uses the expected results of the AccessText class

Today I went through the AccessText class. I could not determine the exact use and results of using this class.

If you use an AccessText with a label and use the Target property as a TextBox , the TextBox will get focus when you press the access key . Below is the 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 Label and use the Target property as Buton Button Click event will AccessText when the access key pressed.

So my questions

1. What is the final behavior of the AccessText class? If I should predict that it will use other types of controls, such as DataGrid , ComboBox , RadioButton ? How can I be sure of the expected result?

2. Why is it a class derived from FrameworkElement ? What applications does it have as a FrameworkElement ? seems a bit bigger for just pointing out Accesskeys etc.

+8
c # data-binding wpf xaml
source share
1 answer

AccessText is a FrameworkElement that more or less looks like a special TextBlock type that allows any keyboard character to follow one underscore ( _ ) as an access key.

For this control, the behavior of the associated access keys depends on the OnAccessKey method. OnAccessKey is a virtual UIElement method that provides the following definition:

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

That way, any control that does not override the OnAccessKey definition defined by UIElement will support the default behavior that is needed to keep the control focused when the access key is pressed.

ButtonBase that Button inherits has the following definition for OnAccessKey :

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

Thus, the default behavior of Button and other controls that inherit from ButtonBase will direct control to focus if IsMultiple true, otherwise it will raise a click event. ( IsMultiple true if the passkey is associated with more than one UIElement .)

With that in mind, here are the answers to your specific questions:

  • The defining behavior of the AccessText element used as the ContentPresenter control is to register the first letter following the single underscore with the AccessKeyManager , which will call the OnAccessKey control when the key is pressed. Knowing what this will do for a particular control, you need to know which OnAccessKey definition OnAccessKey valid for that control. If there are no overrides in the inheritance chain, pressing the access key will focus the control. If there is an override, the behavior will depend on the definition of the override method. This can be determined through experimentation, reading relevant documentation, or by studying the source code.

  • AccessText is a FrameworkElement for the same reasons that a TextBlock is a FrameworkElement . It has a visual form and occupies a space that the layout system must take into account when placing other elements with respect to it. In addition, FrameworkElements allows for styling and has its own DataContext property, which allows you to use binding scripts that would otherwise be impossible. If AccessText were not FrameworkElement , this would be unnecessarily restrictive and would prevent sensible (albeit perhaps rare) uses of WPF developers.

Edit

Here's an example of a fancy power button that demonstrates the utility of AccessText as a 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> 

As a result (after pressing Alt ):

enter image description here

After pressing the button or pressing Alt + S, the view model will respond to the command by changing Text and Fill , as a result, the following will be obtained:

enter image description here

Clicking or using the passkey will return to the first state again.

+11
source share

All Articles