1

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

<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"));
}
4

2 回答 2

2

如何将样式声明为 a并使用类似这样的标记扩展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位于 的 Visual Tree 中的某个位置Button,您可以使用RelativeSourcein 样式而不是使用ElementNamein 绑定这样的东西 -

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

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

更新 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>
于 2012-11-25T18:26:17.727 回答
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}"

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

于 2012-11-25T21:45:37.807 回答