比较DataTrigger中的两个动态值 [英] comparing two dynamic values in DataTrigger
问题描述
我想比较两个动态值 User_id
和 user_id
的相等性并设置一个属性光标
。另外,当光标是手时,我必须执行一个功能。怎么做?这是我正在使用的代码:
I want to compare two dynamic values User_id
and user_id
for 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:
<DataTrigger Binding="{Binding Path=User_id}" Value="{Binding Path=user_id}">
<Setter Property="Cursor" Value="Hand"/>
</DataTrigger>
推荐答案
有两种方法可以解决这个问题。
There are a couple options to attack this.
您可以使用 Multibinding
将这两个值输入到 IMultiValueConverter code>。要在
DataTrigger
中使用这种类型的绑定,请使用以下语法。
You can use Multibinding
to input the two values into a IMultiValueConverter
. To use this type of binding in your DataTrigger
, you would use follow the following syntax.
<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>
MultiBinding.Converter
设置为 EqualityConverter
的新实例,这是我创建的实现 IMultiValueConverter
接口的类。本课程将为您进行比较。 DataTrigger
在此转换器返回true时触发。
The MultiBinding.Converter
is set to a new instance of EqualityConverter
, which is a class I created that implements the IMultiValueConverter
interface. This class will do the comparison for you. The DataTrigger
triggers when this converter returns 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模式
我不确定您的 DataContext
的来源,但如果可能,您可能想要考虑将视图模型用于绑定。视图模型可以公开为您执行相等比较的属性。
#2. MVVM Pattern
I'm not sure where your DataContext
is 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.
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.
}
}
}
如果使用这样的视图模型,您的 DataTrigger
可以简化为。.
If using a view model like this, your DataTrigger
could simplify to..
<DataTrigger Binding="{Binding Path=IsUserIdsEqual}" Value="True">
<Setter Property="Window.Cursor" Value="Hand"/>
</DataTrigger>
关于在触发器上执行功能,我添加了 DoSomething
方法突出显示当两个ID相等时如何使用视图模型执行函数。我不确定这是否适合您的情况,因为我不确定函数调用的目的是什么,但这是在条件改变时执行函数的一种方式。
Regarding executing a function on the trigger, I added a DoSomething
method 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.
这篇关于比较DataTrigger中的两个动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!