【问题标题】:How can I underline a databound value from a DataTemplate?如何在 DataTemplate 中为数据绑定值添加下划线?
【发布时间】:2008-12-11 15:45:21
【问题描述】:

如果我想在 TextBlock 中显示带下划线的值,我必须使用 Run 元素。 (如果有更好/更简单的方法,我很想听听。)

<TextBlock>
  <Run TextDecorations="Underline" Text="MyText" />
</TextBlock>

理想情况下,要在 DataTemplate 中实现它,它看起来像这样:

<DataTemplate x:Key="underlineTemplate">
  <TextBlock>
    <Run TextDecorations="Underline" Text="{Binding Value}" />
  </TextBlock>
</DataTemplate>

但是,这不起作用,因为 Run 的 Text 属性不是 DependencyProperty,因此您无法对其进行数据绑定。有谁知道我如何做到这一点?

【问题讨论】:

    标签: wpf data-binding xaml .net-3.5


    【解决方案1】:

    TextDecoration 是一个附加属性,因此它也可以应用于 TextBlock。您可以通过模板化 TextDecorations 属性创建一些非常酷的效果。

    看到这个MSDN article

    <TextBlock TextDecorations="Underline" Text="{Binding Value}" />
    

    【讨论】:

      【解决方案2】:

      这对我有用:

      <TextBlock Text="MyText" TextDecorations="Underline" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-29
        • 2011-12-09
        • 1970-01-01
        • 2012-04-18
        • 2011-04-13
        • 1970-01-01
        相关资源
        最近更新 更多