使用OR代替AND的MultiDataTrigger [英] MultiDataTrigger with OR instead of AND
问题描述
我正在尝试在我的 Button
上设置多个 DataTriggers
.我做了一些研究,发现 MultiDataTrigger
允许您执行此操作.如果 CCTVPath == string.Empty
或 PermissionsFlag == false,我希望将
.这是我到目前为止所拥有的; Button
的 Visibility
属性设置为false.
I am trying to set multiple DataTriggers
on my Button
. I did some research and found that MultiDataTrigger
allows you to do this. I want the Visibility
property of my Button
to be set to false if the CCTVPath == string.Empty
OR PermissionsFlag == false
. This is what I have so far;
<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
;
And in my code-behind I set PermissionsFlag
like so;
public bool PermissionsFlag { get; set; }
private void OnPageLoaded(object sender, RoutedEventArgs e)
{
PermissionsFlag = false;
}
您可以看到 PermissionsFlag
绝对是错误的,并且肯定有空的 CCTVPath
,但是 Button
从未被隐藏.我在做什么错了?
As you can see PermissionsFlag
is definitely false, and the there are definitely empty CCTVPath
however the Button
is never hidden. What am I doing wrong?
更新:
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
tranform Conditions into two independent 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>
确保绑定路径正确(检查"VS输出"窗口以获取可能的异常消息)
make sure that binding paths are correct (check VS Output window for possible exception messages)
还:不要仅依赖Button的隐藏状态,请在代码(
OnCCTVButtonClick
)中正确实现权限.在这里阅读原因:
also: don't rely only on hidden state of Button, implement permissions properly in code (
OnCCTVButtonClick
). read why here:
自动属性PermissionsFlag( public bool PermissionsFlag {get; set;}
)不会将更改通知给视图.
auto-property PermissionsFlag (public bool PermissionsFlag { get; set; }
) doesn't notify view about changes.
可以实现 INotifyPropertyChanged
接口(在我的测试窗口中是这样完成的:公共部分类Window3:Window,INotifyPropertyChanged
),然后在属性为false时引发事件变化.
it is possible to implement INotifyPropertyChanged
interface (in my test window it is done like this: public partial class Window3 : Window, INotifyPropertyChanged
) and then raise event when property changes.
这是我用于测试的完整工作示例
here is a complete working example which I used for test
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>
这篇关于使用OR代替AND的MultiDataTrigger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!