- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试在我的 Button
上设置多个 DataTriggers
。我做了一些研究,发现 MultiDataTrigger
允许您执行此操作。如果 CCTVPath == string.Empty
或 PermissionsFlag == false,我希望将
。这是我目前所拥有的;Button
的 Visibility
属性设置为 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>
最佳答案
将条件转换为两个独立的数据触发器
<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 输出窗口以获取可能的异常消息)
OnCCTVButtonClick
)。在这里阅读原因:
How to Snoop proof your wpf application?
auto-property 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/
我是一名优秀的程序员,十分优秀!