Disable WPF shortcut accelerator key (no underline text)

I set the .Content value of the label for a string containing underscores; the first underscore is interpreted as an accelerator key.

Without changing the baseline (replacing all _ with __ ), is there a way to disable the accelerator for shortcuts?

+52
user-interface wpf
Sep 02 '08 at 21:32
source share
4 answers

If you use TextBlock as the contents of the label, its text will not accept underscores.

+73
Jul 09 '10 at 8:24
source share

You can override the RecognizesAccessKey ContentPresenter property, which is in the default template for the label. For example:

 <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <Grid.Resources> <Style x:Key="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}" TargetType="Label"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Label"> <Border> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="False" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Grid.Resources> <Label>_This is a test</Label> </Grid> </Page> 
+26
Sep 02 '08 at 22:06
source share

Use <"Text Block">"<"/Text Block"> instead of <"label"><"/label"> to print exact text with underline.

+1
Apr 19 '17 at 13:34 on
source share

Why not?

 public partial class LabelEx : Label { public bool PreventAccessKey { get; set; } = true; public LabelEx() { InitializeComponent(); } public new object Content { get { var content = base.Content; if (content == null || !(content is string)) return content; return PreventAccessKey ? (content as string).Replace("__", "_") : content; } set { if (value == null || !(value is string)) { base.Content = value; return; } base.Content = PreventAccessKey ? (value as string).Replace("_", "__") : value; } } } 
0
May 22 '17 at 15:22
source share



All Articles