wpf 比较 DataTrigger 中的两个动态值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37302270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
comparing two dynamic values in DataTrigger
提问by amit jha
I want to compare two dynamic values User_idand user_idfor equality and setting one property Cursor. Also, when the cursor is hand, I have to execute one function. How to do it? This is the code that I am using:
我想比较两个动态值User_id以及user_id相等和设置一个属性Cursor。另外,当光标在手上时,我必须执行一个函数。怎么做?这是我正在使用的代码:
<DataTrigger Binding="{Binding Path=User_id}" Value="{Binding Path=user_id}">
<Setter Property="Cursor" Value="Hand"/>
</DataTrigger>
回答by Jason Tyler
There are a couple options to attack this.
有几个选项可以解决这个问题。
#1. Multibinding Converter
#1. 多重绑定转换器
You can use Multibindingto input the two values into a IMultiValueConverter. To use this type of binding in your DataTrigger, you would use follow the following syntax.
您可以使用Multibinding将两个值输入到IMultiValueConverter. 要在您的 中使用这种类型的绑定DataTrigger,您将使用以下语法。
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding>
<MultiBinding.Converter>
<local:EqualityConverter />
</MultiBinding.Converter>
<Binding Path="User_id" />
<Binding Path="user_id" />
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Window.Cursor" Value="Hand"/>
</DataTrigger>
The MultiBinding.Converteris set to a new instance of EqualityConverter, which is a class I created that implements the IMultiValueConverterinterface. This class will do the comparison for you. The DataTriggertriggers when this converter returns true.
将MultiBinding.Converter被设置为一个新的实例EqualityConverter,这是一类我创建了一个实现了IMultiValueConverter接口。本课程将为您做比较。该DataTrigger触发器时,该转换器返回true。
public class EqualityConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length < 2)
return false;
return values[0].Equals(values[1]);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
#2. MVVM Pattern
#2. MVVM 模式
I'm not sure where your DataContextis coming from, but if possible, you may want to consider using a view model for your binding. The view model could expose a property that does the equality comparison for you. Something like this.
我不确定您DataContext来自哪里,但如果可能,您可能需要考虑使用视图模型进行绑定。视图模型可以公开一个为您进行相等比较的属性。像这样的东西。
public class UserViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int _User_id;
private int _user_id;
public int User_id
{
get
{
return _User_id;
}
set
{
if (_User_id != value)
{
_User_id = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("User_id"));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsUserIdsEqual"));
DoSomething();
}
}
}
public int user_id
{
get
{
return _user_id;
}
set
{
if (_user_id != value)
{
_user_id = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("user_id"));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsUserIdsEqual"));
DoSomething();
}
}
}
public bool IsUserIdsEqual
{
get { return _user_id == _User_id; }
}
private void DoSomething()
{
if (this.IsUserIdsEqual)
{
//Do something when they are equal.
}
}
}
If using a view model like this, your DataTriggercould simplify to..
如果使用这样的视图模型,您DataTrigger可以简化为..
<DataTrigger Binding="{Binding Path=IsUserIdsEqual}" Value="True">
<Setter Property="Window.Cursor" Value="Hand"/>
</DataTrigger>
Regarding executing a function on the trigger, I added a DoSomethingmethod to highlight how the view model could be used to execute a function when the two IDs are equal. I'm not sure if that would work for your case because I'm not sure what the intent of the function call is, but it is a way to execute a function when a condition changes.
关于在触发器上执行函数,我添加了一个DoSomething方法来突出显示当两个 ID 相等时如何使用视图模型来执行函数。我不确定这是否适用于您的情况,因为我不确定函数调用的意图是什么,但它是一种在条件发生变化时执行函数的方法。

