简体   繁体   English

WPF样式中的DataTrigger绑定

[英]DataTrigger Binding in WPF Style

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 each button should be binded to a different property (SelectedPositions, SelectedAgencies, ....). 我在WPF中有以下Button和Style,我需要在DataTrigger部分中概括Binding,因为我在同一个Window中有近10个类似的按钮,每个按钮应绑定到不同的属性(SelectedPositions,SelectedAgencies,.... )。 Is it possible to implement? 有可能实施吗?

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

    <Style x:Key="NewButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Height" Value="22" />
        <Setter Property="Width" Value="Auto" />
        <Setter Property="FontFamily" Value="OpenSans" />
        <Setter Property="FontSize" Value="13" />
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="Margin" Value="10,2,10,0" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="3">
                        <Grid x:Name="gridButton" Background="#54728e">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Image x:Name="img"
                                   Grid.Column="0"
                                   Width="24"
                                   Height="24"
                                   Source="Img/tick-white.png"
                                   Visibility="Visible" />
                            <Rectangle x:Name="rect"
                                       Grid.Column="1"
                                       Fill="#54728e"
                                       RadiusX="3"
                                       RadiusY="3" />
                            <ContentPresenter Grid.Column="1"
                                              Margin="5,0,5,0"
                                              HorizontalAlignment="Stretch"
                                              VerticalAlignment="Center" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding SelectedPositions}" Value="{x:Static sys:String.Empty}">
                            <Setter TargetName="rect" Property="Fill" Value="#8bbcdf" />
                            <Setter TargetName="img" Property="Visibility" Value="Collapsed" />
                            <Setter TargetName="gridButton" Property="Background" Value="#8bbcdf" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

could you provide me an example of what you explained? 你能给我一个你解释的例子吗?

Sure, 当然,

1 - Using Tag 1 - 使用标签

In your Style have your DataTrigger as: 在你的Style中你的DataTrigger如下:

<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 : 2 - 使用附属物

"local:" refers to the xaml namespace alias of your application or if you use different namespaces, the namespace where MyCustomPropertyCollection is declared. “local:”是指应用程序的xaml名称空间别名,或者如果使用不同的名称空间,则声明MyCustomPropertyCollection的名称空间。

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 3 - 正常依赖属性

custom Button class: 自定义Button类:

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. xaml中的样式不仅需要更新DataTrigger还需要Style定义。

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. Tag方法不受欢迎。 "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. “附加属性”更容易实现,但并不像依赖关系指标那样清晰,因为具有普通DP和AP的自定义类也会过度使用。 Take your pick for what you'd prefer. 选择你喜欢的东西吧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM