Reputation: 1593
I'm working on a WPF window with a ComboBox and have come across the dreaded access key issue. Through my google-fu I've discovered that I can turn it off with the following code;
<Grid.Resources>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="RecognizesAccessKey" Value="False"/>
</Style>
</Grid.Resources>
This works relatively well, when an item is selected, it now shows the underscores and I don't need to worry about any .Replace(...)
calls in the code behind. The problem is that it only works for the selected item, all the other items have the underscore hidden.
In the below picture, I selected AU-15003\SQLEXPRESS_2014
(originally the underscore was hidden), the underscore was shown when the dropdown closed. Then I re-opened the dropdown and the underscore stayed for that item. The other items still have no underscore (SQLEXPRESS2008 follows the same pattern).
I'm not sure why this is, I can't seem to find any other places where I could turn the RecognizesAccessKey property off.
Upvotes: 0
Views: 1664
Reputation: 10349
I'm not sure what you're trying to accomplish here, but I can surely tell you why implicit styles targeting ContentPresenter
tend not to work as expected.
The rule is that templates (ControlTemplates
as well as DataTemplates
) are boundaries for applying implicit styles, i.e. implicit style defined outside of the template will not be applied to elements inside one. There's one exception to that rule - if the targeted type derives from Control
, the style will be applied even inside the templates. And you can easily check that ContentPresenter
does not derive from Control
. Moreover, the ContentPresenter
in question (the one displaying the item in the ComboBox
drop-down) is "located" in ComboBoxItem
's template.
Now I cannot make out anything specific enough to reproduce your issue from the code you've posted, but I can advise you on how to tweak the ContentPresenter
in the ComboBox
drop-down - your best bet is to supply custom ComboBox.ItemTemplate
:
<ComboBox (...)>
<ComboBox.ItemTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding}" RecognizesAccessKey="False" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
If you'd like more general solution (one that resembles your attempt), you could create an implicit style targeting ComboBox
and setting ComboBox.ItemTemplate
property in a similar manner:
<Grid.Resources>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<ContentPresenter Content="{Binding}" RecognizesAccessKey="False" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
Upvotes: 1