我如何重用以下触发器?我需要将它重用于窗口中的所有清除按钮。按钮仅在列表视图中选择项目时可见。所以我需要传递Binding ElementName=teachers
as 参数。有没有办法做到这一点?
<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"));
}