سؤال

I have label on a usercontrol with a property which has a binding. The string format property of this binding needs to be set depending on the data type of the underlying data that is bound to the property of the label's content. So if the property is bound to a date property in my viewmodel, I'd like to specify the string format in a datatrigger. Here is what I have tried but the StringFormat is not being recognised. Am I missing something or am I doing it wrong? Any advise would be greatly appreciated.

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

هل كانت مفيدة؟

المحلول

why not simply use a converter?

 <Label Content="{Binding Path=myProperty, Converter={StaticResource MyContentConverter}}">
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top