简体   繁体   English

使用TextWrapping的WPF CheckBox样式

[英]WPF CheckBox style with the TextWrapping

I need to apply a TextWrapping in the WPF CheckBox . 我需要在WPF CheckBox应用TextWrapping

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. 如果我在CheckBoxContent中使用TextBlock ,则检查元素(垂直对齐为顶部)并且文本正确显示,但不显示加速器。

替代文字

If I use an AccessText in the Content of the CheckBox , the check element displays wrong (vertical alignment is center). 如果我在CheckBoxContent中使用AccessText ,则check元素显示错误(垂直对齐为中心)。

How can I change the Style of the elements to display this CheckBox correct? 如何更改元素的Style以显示此CheckBox是否正确?

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>

Havew you tried setting an implicit style for the AccessText, or just an AccessText style you can apply? 您是否尝试为AccessText设置隐式样式,或者只是可以应用的AccessText样式?

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. 如果在项目中包含此项,则AccessText应按您希望的方式工作。 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: 如果您不希望所有AccessTexts都以这种方式运行,请为该样式命名并将其应用于您使用它的位置:

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

Uss VerticalContentAlignment to align the box to the top. Uss VerticalContentAlignment将框对齐到顶部。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM