WPF Reuseble Style.Triggers

时间:2012-11-25 18:11:16

标签: c# wpf mvvm

我如何重用以下触发器?我需要将其重用于窗口中的所有清除按钮。按钮仅在列表视图中选择项目时可见。所以我需要传递Binding ElementName=teachers作为参数。有没有办法做到这一点?

<Button Width="15"  Grid.Column="1" Content="X" Margin="0,2,5,2" Command="{Binding ClearSubjectCommand}" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Visibility" Value="Visible"></Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=teachers, Path=SelectedItem}" Value="{x:Null}">
                    <Setter Property="Visibility" Value="Collapsed"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

修改

尝试附属物。但没有成功。

<Button Width="15"  Grid.Column="1" Content="X" Margin="0,2,5,2" Command="{Binding ClearSubjectCommand}"
        HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
        local:ThemeProperties.BindingElementName="teachers" Style="{StaticResource cancelButton}"/> 

和风格,

<Style TargetType="{x:Type Button}" x:Key="cancelButton">
    <Setter Property="Visibility" Value="Visible"></Setter>
    <Style.Triggers>
        <DataTrigger Binding="{Binding  ElementName= local:ThemeProperties.BindingElementName, Path=SelectedItem}" Value="{x:Null}">
            <Setter Property="Visibility" Value="Collapsed"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

类,

public static class ThemeProperties
{
    public static string GetBindingElementName(DependencyObject obj)
    {
        return (string)obj.GetValue(BindingElementNameProperty);
    }

    public static void SetBindingElementName(DependencyObject obj, string value)
    {
        obj.SetValue(BindingElementNameProperty, value);
    }

    // Using a DependencyProperty as the backing store for BindingElementName.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BindingElementNameProperty =
        DependencyProperty.RegisterAttached("BindingElementName", typeof(string), typeof(ThemeProperties), new FrameworkPropertyMetadata("teachers"));
}

2 个答案:

答案 0 :(得分:1)

如何将样式声明为Resource并使用StaticResource标记扩展名重新使用它 -

<Window.Resources>
   <Style>
      <Style x:Key="CommonButtonStyle" TargetType="{x:Type Button}">
          <Setter Property="Visibility" Value="Visible"></Setter>
          <Style.Triggers>
              <DataTrigger Binding="{Binding ElementName=teachers,
                                       Path=SelectedItem}" 
                           Value="{x:Null}">
                    <Setter Property="Visibility" Value="Collapsed"></Setter>
              </DataTrigger>
          </Style.Triggers>
      </Style>
   </Style>
</Window.Resources>

并将其用于不同的按钮 -

<Button x:Name="Button1" Style="{StaticResource CommonButtonStyle}"/>
<Button x:Name="Button2" Style="{StaticResource CommonButtonStyle}"/>
<Button x:Name="Button3" Style="{StaticResource CommonButtonStyle}"/>

<强>更新

如果ListView位于Button的可视树中的某个位置,您可以在样式中使用RelativeSource,而不是使用ElementName绑定此类内容 -

<DataTrigger Binding="{Binding Path=SelectedItem, RelativeSource={RelativeSource 
                                        FindAncestor, AncestorType=ListView}}" 
                           Value="{x:Null}">

但是如果它们不相关,则需要使用attached property将参数传递给样式。此链接here可能会帮助您入门。

更新2

我已经使用附加属性但修改了代码的位。这是 -

public static class ThemeProperties
{
    public static object GetSelectedValue(DependencyObject obj)
    {
        return (object)obj.GetValue(SelectedValueProperty);
    }

    public static void SetSelectedValue(DependencyObject obj, object value)
    {
        obj.SetValue(SelectedValueProperty, value);
    }

    public static readonly DependencyProperty SelectedValueProperty =
        DependencyProperty.RegisterAttached("SelectedValue", typeof(object), 
          typeof(ThemeProperties), new FrameworkPropertyMetadata(null, 
           FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));     
}

XAML -

    <Style TargetType="{x:Type Button}" x:Key="cancelButton">
        <Setter Property="Visibility" Value="Visible"/>
        <Style.Triggers>
            <Trigger Property="local:ThemeProperties.SelectedValue"
                         Value="{x:Null}">
                <Setter Property="Visibility" Value="Collapsed"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

这是ListView和按钮 -

   <StackPanel>
        <ListView x:Name="lstView" ItemsSource="{Binding Objects}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button VerticalAlignment="Bottom" Height="30"  Width="100"
                local:ThemeProperties.SelectedValue="{Binding SelectedItem,
                            ElementName=lstView}"
                Style="{StaticResource cancelButton}"/>
    </StackPanel>

答案 1 :(得分:0)

我真的很抱歉我花了这么久。但最后我明白了。我使用自定义转换器,我知道它不是你想要的,但它工作得很好。它提供了您想要的功能。

首先,我的 SelectionToVisibilityConverter

public class SelectionToVisibilityConverter : IValueConverter
{
    public object Convert(
        object value,
        Type targetType,
        object parameter,
        CultureInfo culture)
    {
        if (value == null)
        {
            return Visibility.Collapsed;
        }
        return Visibility.Visible;
    }

    public object ConvertBack(
        object value,
        Type targetType,
        object parameter,
        CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

以下是我的观点:

    <UserControl.Resources>
    <Test:SelectionToVisibilityConverter x:Key="VisibilityConverter" />
</UserControl.Resources>
<Grid>
    <ListView Height="100" HorizontalAlignment="Left" Margin="29,29,0,0" Name="listView1" ItemsSource="{Binding Path=TestItems}" SelectedItem="{Binding Path=TestItemsSelectedItem}" VerticalAlignment="Top" Width="120" />
    <ListView Height="100" HorizontalAlignment="Left" Margin="155,29,0,0" Name="listView2" ItemsSource="{Binding Path=TestItems2}" SelectedItem="{Binding Path=TestItems2SelectedItem}"  VerticalAlignment="Top" Width="120" />
    <Button Content="Button" Command="{Binding Path=ReloadCommand}" Visibility="{Binding ElementName=listView1, Path=SelectedItem, Converter={StaticResource VisibilityConverter}}" Height="23" HorizontalAlignment="Left" Margin="29,135,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    <Button Content="Button" Command="{Binding Path=ReloadCommand}" Visibility="{Binding ElementName=listView2, Path=SelectedItem, Converter={StaticResource VisibilityConverter}}" Height="23" HorizontalAlignment="Left" Margin="155,135,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
</Grid>

请忽略ListViews上的SelectedItem和按钮上的ReloadCommand。我用它来测试这个。每次选择某个ListView的项目时,都会重新评估相应按钮的可见性。

修改

我的最后一个想法是创建自定义控件并为此控件定义样式/模板。像这样:

    <Style x:Key="CustomControlStyle" TargetType="{x:Type Test:MyFirstCustomControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Test:MyFirstCustomControl}">
                    <Grid>
                        <ListView Height="100" HorizontalAlignment="Left" Margin="5, 5, 0, 0" Name="listView1" VerticalAlignment="Top" Width="120" />
                        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="50,110,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding ElementName=listView, Path=SelectedItem}" Value="{x:Null}">
                            <Setter Property="Visibility" TargetName="button1" Value="Collapsed"></Setter>
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

这样,触发器将在您设置

的所有MyFirstConstomControl之间共享
Style="{StaticResource CustomControlStyle}"

我认为这不容易,但我不知道更好,所以我只是把我得到的东西带来。