Cannot see _ (underscore) in WPF content

后端 未结 5 1015
挽巷
挽巷 2020-12-03 20:34

A very simple question:

Why I cant see _ (underscore) in WPF content?

For instance the content of

相关标签:
5条回答
  • 2020-12-03 21:06

    I know im late to the party but I believe that if you don't have the Label associated to a TextBox than you should use a TextBlock instead.

    Changing your control to a TextBlock solves this issue since only Label has the mnemonic support

    0 讨论(0)
  • 2020-12-03 21:16

    This is because Label supports defining a mnemonic based on its content, which is done by prefixing the mnemonic with an underscore (the same thing that happens in Windows Forms with &).

    Use a double underscore if you want a literal one to appear:

    <Label Content="test__t" Name="label2"  />
    
    0 讨论(0)
  • 2020-12-03 21:17

    I was binding to a data source which I didn't want to change, so I used a custom converter to create the missing underscore:

    [ValueConversion(typeof(string), typeof(string))]
    public class ShowUnderscoreConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
            value is string text ? text.Replace("_", "__") : null;
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => 
            value is string text ? text.Replace("__", "_") : null;
    }
    
    0 讨论(0)
  • 2020-12-03 21:20

    Labels support mnemonics (i.e. you can use ctrl+(key) to give them focus). You define the mnemonic key using an underscore.

    http://www.charlespetzold.com/blog/2006/01/061004.html

    If you want to see underscores, replace single underscores with double underscores.

    0 讨论(0)
  • 2020-12-03 21:28

    This style solves your problem:

    <Style x:Key="{x:Type Label}"
       TargetType="{x:Type Label}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Label}">
                <Border Background="{TemplateBinding Background}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Padding="{TemplateBinding Padding}"
                        SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      RecognizesAccessKey="False"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled"
                             Value="false">
                        <Setter Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    

    0 讨论(0)
提交回复
热议问题