在 WPF DataTrigger 值中绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17000901/
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
Binding in WPF DataTrigger value
提问by Tanuj Wadhwa
Well, this may be an easy question but I am not able to find a solution for this.
好吧,这可能是一个简单的问题,但我无法找到解决方案。
I have a DataTriggeras
我有一个DataTrigger为
<DataTrigger Binding="{Binding Quantity}" Value="0">
Now I want to bind Valueto a variable myVariable. So if the value of myVariablechanges, the Valueproperty of the DataTrigger changes too.
现在我想绑定Value到一个变量myVariable。因此,如果值发生myVariable变化,ValueDataTrigger的属性也会发生变化。
I have tried setting Binding but it I guess its not possible to set it. Is there any other method by which I can set this Value dynamically.
我试过设置绑定,但我想它不可能设置它。有没有其他方法可以动态设置这个值。
EDIT : I tried creating a data trigger of my own. But I am still not able to get things working.
编辑:我尝试创建自己的数据触发器。但我仍然无法让事情发挥作用。
This is the code :
这是代码:
DataTrigger d = new DataTrigger();
d.Binding = new Binding("Quantity");
d.Value = 1;
Setter s = new Setter(BackgroundProperty, Brushes.Red);
d.Setters.Add(s);
Style st = new Style(typeof(DataGridRow));
st.Triggers.Add(d);
this.Resources.Add(this.Resources.Count, st);
I want to use the above code in place of following xaml
我想用上面的代码代替下面的 xaml
<Page.Resources>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Quantity}" Value="1">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</Page.Resources>
Thanks.
谢谢。
采纳答案by Colin
To my understanding of your problem, you are trying to find a way to set the Valueof your DataTriggeraccording to the value of one of the properties of your view model. So here I have a solution.
要我对你的问题的理解,你正在努力寻找一种方法来设置Value您DataTrigger根据您的视图模型的属性之一的值。所以在这里我有一个解决方案。
Here is the View Model
这是视图模型
public class ViewModel : INotifyPropertyChanged
{
private string _text;
private string _candidateValue;
public string Text
{
get
{
return this._text;
}
set
{
this._text = value;
if (null != PropertyChanged)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("Text"));
}
}
}
public string CandidateValue
{
get
{
return this._candidateValue;
}
set
{
this._candidateValue = value;
if (null != PropertyChanged)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("Text"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
And you need a IValueConverter in your binding of DataTrigger
并且您在 DataTrigger 的绑定中需要一个 IValueConverter
public class ValueConverter : DependencyObject, IValueConverter
{
public static readonly DependencyProperty CandidateValueProperty = DependencyProperty.Register("CandidateValue", typeof(string), typeof(ValueConverter));
public string CandidateValue
{
get { return (string)GetValue(CandidateValueProperty); }
set { SetValue(CandidateValueProperty, value); }
}
public ValueConverter()
: base()
{
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (null != value)
{
if (value.ToString() == this.CandidateValue)
return true;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
And the xaml is quite simple
而且 xaml 很简单
<TextBox x:Name="TextBox" Text="{Binding Text,UpdateSourceTrigger=PropertyChanged}">
</TextBox>
You need to create your datatrigger in code behind.
您需要在后面的代码中创建数据触发器。
public MainWindow()
{
InitializeComponent();
//The view model object
ViewModel vm = new ViewModel();
vm.CandidateValue = "1";
this.DataContext = vm;
//create data trigger object for TextBox
DataTrigger d = new DataTrigger();
//create binding object for data trigger
Binding b = new Binding("Text");
ValueConverter c = new ValueConverter();
b.Converter = c;
//create binding object for ValueConverter.CandidateValueProperty
Binding convertBinding = new Binding("CandidateValue");
convertBinding.Source = vm;
BindingOperations.SetBinding(c, ValueConverter.CandidateValueProperty, convertBinding);
d.Binding = b;
d.Value = true;
Setter s = new Setter(TextBox.ForegroundProperty, Brushes.Red);
d.Setters.Add(s);
Style st = new Style(typeof(TextBox));
st.Triggers.Add(d);
this.TextBox.Style = st;
}
The trick here is that the ValueConverterhas a dependency property named CandidateValueProperty. And this property is bind to the CandidateValueof view model. So the foreground of the TextBox will be red when the input value equals to CandidateValue of view model.
这里的技巧是ValueConverter有一个名为 的依赖属性CandidateValueProperty。此属性绑定到CandidateValue视图模型。所以当输入值等于视图模型的CandidateValue时,TextBox的前景将是红色的。

