WPF TextBlock不显示下划线

时间:2017-03-09 15:32:20

标签: c# wpf textblock

我在下拉控件中有这段代码:

    <ComboBox.Template>
        <ControlTemplate TargetType="ComboBox">
            <Grid>
                <ToggleButton x:Name="ToggleButton" 
                              Grid.Column="2" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                              Focusable="false"                           
                              ClickMode="Press" 
                              HorizontalContentAlignment="Left" >
                    <ToggleButton.Template>
                        <ControlTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="18"/>
                                </Grid.ColumnDefinitions>

   . . . . 

                                <Border x:Name="BorderComp" 
                                        Grid.Column="0"
                                        CornerRadius="0" 
                                        Margin="1" 
                                        Background="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}"
                                        BorderBrush="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" BorderThickness="0,0,0,0" >

                                      <TextBlock Text="{Binding Path=Text,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" 
                                                 Background="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}"
                                                 Padding="2" />
                                </Border>

当要显示的文本包含下划线时,下划线不会显示在屏幕上。我使用Snoop检查值,Text值确实包含下划线。例如,X_9999显示为X9999

我发现有很多帖子说明下划线用作Labels上的加速器,但这不应该影响TextBlock

有什么想法吗?

更新

我发现问题属于ComboBox.ItemTemplate,其定义如下:

    <ComboBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Title}"
                      IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"
                      Tag="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}"
                      Click="CheckBox_Click" />
            </CheckBox>
        </DataTemplate>
    </ComboBox.ItemTemplate>

我改为以下内容,现在一切正常。显然,Checkbox默认使用Label

    <ComboBox.ItemTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"
                      Tag="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}"
                      Click="CheckBox_Click">
                <TextBlock Text="{Binding Title}" />
            </CheckBox>
        </DataTemplate>
    </ComboBox.ItemTemplate>

0 个答案:

没有答案