2010-06-01 29 views
14

我需要在WPF CheckBox中应用TextWrapping带有TextWrapping的WPF CheckBox样式

请看看这两个样本:

<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> 

如果我在CheckBox,检查元素(垂直对齐顶部)和文本显示正常,但没有加速的Content使用TextBlock

alt text

如果我在CheckBoxContent使用AccessText,单向元件显示错误(垂直取向是中央)。

如何更改Style的元素以正确显示此CheckBox

+3

一个人的垃圾是另一个人的宝藏。我想弄清楚如何使我的检查元素垂直居中关于文本,因为VerticalAlignment和VerticalContentAlignment不起作用。答:我只需要使用AccessText而不是TextBlock! +1提示:) – Qwertie 2011-08-02 21:22:24

+0

不客气。但我在这里的问题仍然存在...... – 2011-08-02 23:54:20

回答

13

如果将两者结合起来,您可能会得到所需的效果。

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

似乎它不适用于3.5sp1 – 2010-06-01 15:12:17

+0

我试过了,它不起作用。您可以使用例如XamlPad非常快地尝试它。 – 2010-06-02 09:19:42

0

Havew您尝试设置一个隐含的作风为AccessText,或只是一个AccessText风格,你可以申请?

这里有一个隐含的风格,将工作:

<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> 

如果包含此项目中的AccessText应该工作,你想要的方式。如果你需要别的东西,请调整风格。

如果您不希望所有AccessTexts表现这种方式,命名风格和运用它,你使用它:

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

不幸的没有像TextBlock那样工作。 – 2010-11-24 12:47:12