WPF CheckBox style with the TextWrapping

*爱你&永不变心* 提交于 2020-01-02 00:44:11

问题


I need to apply a TextWrapping in the WPF CheckBox.

Please look at this two samples:

<CheckBox>  
  <TextBlock TextWrapping="Wrap"  
             Text="_This is a long piece of text attached to a checkbox."/>  
</CheckBox>

<CheckBox>  
  <AccessText TextWrapping="Wrap"  
              Text="_This is a long piece of text attached to a checkbox."/>  
</CheckBox>

If I use a TextBlock in the Content of the CheckBox, the check element (vertical alignment is top) and the text displays properly, but not the accelerator.

If I use an AccessText in the Content of the CheckBox, the check element displays wrong (vertical alignment is center).

How can I change the Style of the elements to display this CheckBox correct?


回答1:


If you combine the two you will probably get the effect you desire.

<CheckBox>
    <TextBlock>
        <AccessText TextWrapping="Wrap"  
                    Text="_This is a long piece of text attached to a checkbox."/>  
    </TextBlock>
</CheckBox>



回答2:


Havew you tried setting an implicit style for the AccessText, or just an AccessText style you can apply?

Here's an implicit style that would work:

    <Style x:Key="{x:Type AccessText}" 
    TargetType="{x:Type AccessText}"
    BasedOn="{x:Null}">
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="FontFamily" Value="Segoe UI"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
    <Setter Property="TextWrapping" Value="NoWrap"/>
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="VerticalAlignment" Value="Top"/>
    <Setter Property="Margin" Value="5,2"/>
    <Setter Property="Text" Value="AccessText"/>
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="Gray"/>
        </Trigger>
    </Style.Triggers>
</Style>

If you include this in your project the AccessText should work the way you want. If you need something else, adjust the style.

If you don't want all AccessTexts to behave this way, name the style and apply it where you use it:

<CheckBox>         
  <AccessText TextWrapping="Wrap" Style="{DynamicResource CkbxAccessTextStyle}"        
              Text="_This is a long piece of text attached to a checkbox."/>         
</CheckBox> 



回答3:


Uss VerticalContentAlignment to align the box to the top. Use Padding to adjust text position.

<CheckBox VerticalContentAlignment="Top" Padding="2,-2,0,0">
    <AccessText Text="_This is a long piece of text attached to a checkbox." TextWrapping="Wrap"/>
</CheckBox>


来源:https://stackoverflow.com/questions/2950240/wpf-checkbox-style-with-the-textwrapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!