wpf 使用OR代替AND的MultiDataTrigger

z4iuyo4d  于 2022-11-18  发布在  其他
关注(0)|答案(3)|浏览(198)

我试图在我的Button上设置多个DataTriggers。我做了一些研究,发现MultiDataTrigger允许你这样做。我希望我的ButtonVisibility属性被设置为false,如果CCTVPath == string.EmptyPermissionsFlag == 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>
htrmnn0y

htrmnn0y1#

将条件转换为两个独立的DataTrigger

<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输出 windows 中可能的例外状况消息)
另外:不要只依赖按钮隐藏状态,在代码(OnCCTVButtonClick)中正确实现权限。请在此处阅读原因:
How to Snoop proof your wpf application?
auto-property PermissionsFlag(public bool PermissionsFlag { get; set; })不会通知视图有关更改。
可以实现INotifyPropertyChanged接口(在我的测试窗口中,它是这样完成的: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>
sshcrbum

sshcrbum2#

另一种解决方案是使用一个DataTrigger和一个MultiBinding。您可以通过定义一个“特殊情况”IMultiValueConverter来使其工作,该转换器假定对象数组中有两项,如果第一项是空字符串或第二项是false,则返回true。但是,您可能永远不会在代码的其他任何地方使用该转换器。因此,如果您愿意在前面多做一些工作,您可以定义3个简单/可重用的转换器。
1)一个[IMultiValueConverter] 'OrConverter',它看起来像这样:

public class BooleanOrConverter : IMultiValueConverter {
   public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
      return values.OfType<bool>().Any(b => b);
   }

   public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
      throw new NotImplementedException();
   }
}

2)[IValueConverter]“IsNullOrEmpty”字符串转换器:

public class StringIsNullOrEmptyConverter : IValueConverter {
   public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
      return string.IsNullOrEmpty(value as string);
   }

   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
      throw new NotImplementedException();
   }
}

3)和[IValueConverter] '非转换器:'

public class BooleanNotConverter : IValueConverter {
   public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
      return !(bool)value;
   }

   public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
      throw new NotImplementedException();
   }
}

然后,在您的xaml中,DataTrigger的定义如下:

<Button x:Name="cctvFeedButton" Content="Live Feed"
  Width="100" FontSize="16" HorizontalAlignment="Right" Margin="5">
  <Button.Style>
    <Style TargetType="Button">
      <Style.Triggers>
         <DataTrigger Value="True">
           <DataTrigger.Binding>
             <MultiBinding Converter="{StaticResource OrConverter}">
               <Binding Path="PermissionFlag" Converter="{StaticResource NotConverter}"/>
               <Binding Path="CCTVPath" Converter="{StaticResource IsNullOrEmptyConverter}"/>
             </MultiBinding>
           </DataTrigger.Binding>
           <Setter Property="Visibility" Value="Hidden"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>

我更喜欢这种解决方案,而不是使用2个单独的DataTrigger来提高可读性;它更好地表达了您正在定义的行为-它是“或”逻辑:应该隐藏按钮的2个条件的单一集合。

i34xakig

i34xakig3#

当文本框为空时,我使用这些触发器禁用“添加”按钮:
或(如果其中一个文本框被填充,按钮将启用)

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding Text, ElementName=Usernametxtbox}" Value=""/>
        <Condition Binding="{Binding Password, ElementName=passtxtbox}" Value=""/>
    </MultiDataTrigger.Conditions>
    <Setter Property="IsEnabled" Value="False" />
</MultiDataTrigger>

AND(如果两个文本框都已填写,则按钮将启用)

<DataTrigger Binding="{Binding Text, ElementName=Usernametxtbox}" Value="">
    <Setter Property="IsEnabled" Value="False" />
</DataTrigger>

<DataTrigger Binding="{Binding Password, ElementName=passtxtbox}" Value="">
    <Setter Property="IsEnabled" Value="False" />
</DataTrigger>

相关问题