wpf - 绑定(bind)属性上的 DataTrigger

标签 wpf binding datatrigger

我在用户控件上有一个带有绑定(bind)属性的标签。需要根据绑定(bind)到标签内容属性的基础数据的数据类型来设置此绑定(bind)的字符串格式属性。因此,如果该属性绑定(bind)到我的 View 模型中的日期属性,我想在数据触发器中指定字符串格式。这是我尝试过的,但 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>

最佳答案

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

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

关于wpf - 绑定(bind)属性上的 DataTrigger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17230847/

相关文章:

c# - TestStack.White 没有正确返回元素

wpf - 尝试查找功能区控件资源字典时出错 - Office2007Blue

Java 将 bean 绑定(bind)到 String 空属性

c# - DataTrigger 绑定(bind)到属性

c# - 将所选项目写入 ObservableCollection

c# - 如何将列表列表绑定(bind)到 WPF 中的列表框?

wpf - 如何更改 HierarchicalDataTemplate?

c# - 如何根据条件将 VIew 模型的属性绑定(bind)到 DataTrigger Setter?

asp.net - 在运行时获取 IIS 绑定(bind)

c# - WPF:如何使用 MVVM 将命令绑定(bind)到 ListBoxItem?