DataTrigger Binding in WPF Style

前端 未结 1 770
太阳男子
太阳男子 2020-12-18 22:41

I have the following Button and Style in WPF and I need to generalize the Binding in the DataTrigger section because I have near 10 similar buttons in the same Window and ea

相关标签:
1条回答
  • 2020-12-18 22:55

    could you provide me an example of what you explained?

    Sure,

    1 - Using Tag

    In your Style have your DataTrigger as:

    <DataTrigger Binding="{Binding Path=Tag,
                                    RelativeSource={RelativeSource Self}}"
                  Value="{x:Static sys:String.Empty}">
      ...
    </DataTrigger>
    

    as for usage:

    <Button x:Name="btnPosition"
                Grid.Row="0"
                Grid.Column="0"
                HorizontalAlignment="Left"
                VerticalAlignment="Center"
                Command="{Binding PositionFilterCommand}"
                Content="{l:Translate position}"
                Tag="{Binding SelectedPositions}"
                Style="{StaticResource NewButtonStyle}" />
    

    2 - Using Attached Property:

    "local:" refers to the xaml namespace alias of your application or if you use different namespaces, the namespace where MyCustomPropertyCollection is declared.

    code-behind:

    public class MyCustomPropertyCollection {
      public static readonly DependencyProperty SomeStringProperty =
        DependencyProperty.RegisterAttached(
          "SomeString",
          typeof(string),
          typeof(MyCustomPropertyCollection),
          new FrameworkPropertyMetadata(string.Empty));
    
      public static void SetSomeString(UIElement element, string value) {
        element.SetValue(SomeStringProperty, value);
      }
    
      public static string GetSomeString(UIElement element) {
        return (string)element.GetValue(SomeStringProperty);
      }
    }
    

    Style.DataTrigger

    <DataTrigger Binding="{Binding Path=(local:MyCustomPropertyCollection.SomeString),
                                    RelativeSource={RelativeSource Self}}"
                  Value="{x:Static sys:String.Empty}">
      ...
    </DataTrigger>
    

    usage:

    <Button x:Name="btnPosition"
                Grid.Row="0"
                Grid.Column="0"
                HorizontalAlignment="Left"
                VerticalAlignment="Center"
                Command="{Binding PositionFilterCommand}"
                Content="{l:Translate position}"
                local:MyCustomPropertyCollection.SomeString="{Binding SelectedPositions}"
                Style="{StaticResource NewButtonStyle}" />
    

    3 - Normal Dependency Property

    custom Button class:

    public class MyButton : Button {
      public static readonly DependencyProperty SomeStringProperty =
        DependencyProperty.Register(
          "SomeString",
          typeof(string),
          typeof(MyButton),
          new FrameworkPropertyMetadata(string.Empty));
    
      public string SomeString {
        get {
          return (string)GetValue(SomeStringProperty);
        }
        set {
          SetValue(SomeStringProperty, value);
        }
      }
    }
    

    Style in xaml not only needs DataTrigger updated but Style definition too.

    so switch

    <Style x:Key="NewButtonStyle" TargetType="{x:Type Button}">
    

    to

    <Style x:Key="NewButtonStyle" TargetType="{x:Type local:MyButton}">
    

    Style.DataTrigger

    <DataTrigger Binding="{Binding Path=SomeString,
                                    RelativeSource={RelativeSource Self}}"
                  Value="{x:Static sys:String.Empty}">
      ...
    </DataTrigger>
    

    usage:

    <local:MyButton x:Name="btnPosition"
                Grid.Row="0"
                Grid.Column="0"
                HorizontalAlignment="Left"
                VerticalAlignment="Center"
                Command="{Binding PositionFilterCommand}"
                Content="{l:Translate position}"
                SomeString="{Binding SelectedPositions}"
                Style="{StaticResource NewButtonStyle}" />
    

    Tag approach is frowned upon. "Attached Property" is easier to implement but isn't as clear of a indicator of dependencies as a custom class with a normal DP and AP also gets way overused. Take your pick for what you'd prefer.

    0 讨论(0)
提交回复
热议问题