【问题标题】:DataTrigger on a Property of a Binding绑定属性上的 DataTrigger
【发布时间】:2013-06-18 07:48:48
【问题描述】:

我在用户控件上有一个带有绑定属性的标签。此绑定的字符串格式属性需要根据绑定到标签内容属性的基础数据的数据类型进行设置。因此,如果该属性绑定到我的视图模型中的日期属性,我想在数据触发器中指定字符串格式。这是我尝试过的,但 StringFormat 未被识别。我错过了什么还是我做错了什么?任何建议将不胜感激。

<Label
    x:Name="myLabel"
    Content="{Binding Path=myProperty}">
<Label.Style>
  <Style>
    <Style.Triggers>
      <DataTrigger
            Binding="{Binding ElementName=myLabel, Path=Content.Binding}"
            Value="{x:Type sys:DateTime}">
                  <Setter
                     Property="StringFormat"
                     Value="dd/MM/yyyy" />
     </DataTrigger>
   </Style.Triggers>
  </Style>
</Label.Style>

【问题讨论】:

    标签: wpf binding datatrigger


    【解决方案1】:

    为什么不简单地使用转换器?

     <Label Content="{Binding Path=myProperty, Converter={StaticResource MyContentConverter}}">
    

    【讨论】:

    • 这实际上是一个更好的主意,呵呵!为什么我没想到。我会试一试,让你知道。谢谢!
    • 我的转换器的代码如下所示: public object Convert(object value, Type targetType, object parameter, CultureInfoculture) { if (value is DateTime) return ((DateTime)value).ToString(" dd/MM/yyyy");返回值; } 奇迹般有效!感谢您的帮助!
    猜你喜欢
    • 2016-10-28
    • 2013-06-25
    • 2023-04-07
    • 2013-12-14
    • 2013-09-05
    • 1970-01-01
    • 2011-01-15
    • 2012-04-12
    • 2012-01-18
    相关资源
    最近更新 更多