我正在尝试在自己的DataTriggers
上设置多个Button
。我做了一些研究,发现MultiDataTrigger
允许您执行此操作。如果Visibility
或Button
,我希望将自己的CCTVPath == string.Empty
的PermissionsFlag == false
属性设置为false。到目前为止,这就是我所拥有的。
<Button Grid.Column="3" x:Name="cctvFeedButton" Content="Live Feed"
Width="100" FontSize="16" HorizontalAlignment="Right" Margin="5" Click="OnCCTVButtonClick">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding CCTVPath}" Value=""/>
<Condition Binding="{Binding PermissionsFlag}" Value="False"/>
</MultiDataTrigger.Conditions>
<Setter Property="Visibility" Value="Hidden"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
在我的代码后面,我像这样设置
PermissionsFlag
;public bool PermissionsFlag { get; set; }
private void OnPageLoaded(object sender, RoutedEventArgs e)
{
PermissionsFlag = false;
}
如您所见,
PermissionsFlag
绝对是错误的,并且肯定有空的CCTVPath
,但是Button
从未被隐藏。我究竟做错了什么?更新:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private bool _permissionsFlag;
public bool Flag
{
get { return _permissionsFlag; }
set
{
_permissionsFlag = value;
OnPropertyChanged("PermissionsFlag");
}
}
private void OnPageLoaded(object sender, RoutedEventArgs e)
{
Flag = false;
CCTVImageCollection = GetImages();
imageListBox.ItemsSource = CCTVImageCollection;
DataContext = this;
}
在我的XAML中:
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding PermissionsFlag}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
最佳答案
将条件转换为两个独立的DataTriggers
<Style.Triggers>
<DataTrigger Binding="{Binding CCTVPath}" Value="">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
<DataTrigger Binding="{Binding PermissionsFlag}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
确保绑定(bind)路径正确(检查“VS输出”窗口以获取可能的异常消息)
还:不仅仅依赖Button的隐藏状态,在代码(
OnCCTVButtonClick
)中正确实现权限。在这里阅读原因:How to Snoop proof your wpf application?
自动属性PermissionsFlag(
public bool PermissionsFlag { get; set; }
)不会将更改通知 View 。可以实现
INotifyPropertyChanged
接口(interface)(在我的测试窗口中是这样完成的:public partial class Window3 : Window, INotifyPropertyChanged
),然后在属性更改时引发事件。这是我用来测试的完整的工作示例
public partial class Window3 : Window, INotifyPropertyChanged
{
public Window3()
{
InitializeComponent();
this.DataContext = this;
//PermissionsFlag = true;
CCTVPath = "youtube.com";
}
private bool _permissionsFlag = false;
private string _cctvPath;
public bool PermissionsFlag
{
get { return _permissionsFlag; }
set
{
_permissionsFlag = value;
OnPropertyChanged("PermissionsFlag");
}
}
public string CCTVPath
{
get { return _cctvPath; }
set
{
_cctvPath = value;
OnPropertyChanged("CCTVPath");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
窗口xaml:
<Window x:Class="WpfDemos.Views.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window3" Height="300" Width="300">
<StackPanel>
<CheckBox Name="chkPermissionsFlag"
Content="PermissionsFlag"
IsChecked="{Binding Path=PermissionsFlag, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Text="{Binding Path=CCTVPath, UpdateSourceTrigger=PropertyChanged}"/>
<Button x:Name="cctvFeedButton" Content="Live Feed"
Width="100" FontSize="16" HorizontalAlignment="Right" Margin="5">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=CCTVPath}" Value="">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=PermissionsFlag}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</StackPanel>
</Window>
关于c# - 使用OR代替AND的MultiDataTrigger,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38396419/