在WPF DataTrigger值绑定

编程入门 行业动态 更新时间:2024-10-18 10:23:50
本文介绍了在WPF DataTrigger值绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

那么,这可能是一个简单的问题,但我不能够找到一个解决方案。

Well, this may be an easy question but I am not able to find a solution for this.

我有一个 DataTrigger 为

<DataTrigger Binding="{Binding Quantity}" Value="0">

现在我要绑定值来一个变量 myVariable的。因此,如果对 myVariable的的值发生变化时,DataTrigger的值属性也会发生变化。

Now I want to bind Value to a variable myVariable. So if the value of myVariable changes, the Value property of the DataTrigger changes too.

我已经尝试设置绑定,但我想它不可能设置。有没有通过它我可以动态地设置此值的任何其他方法。

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.

这是在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);

我想用上面的code代替下面的XAML的

I want to use the above code in place of following 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>

感谢。

推荐答案

要我对你的问题的理解,你正在努力寻找一种方法来设置值的你 DataTrigger 根据您的视图模型的属性之一的价值。所以在这里我有一个解决方案。

To my understanding of your problem, you are trying to find a way to set the Value of your DataTrigger according to the value of one of the properties of your view model. So here I have a solution.

下面是视图模型

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; }

和你需要在你DataTrigger的绑定的IValueConverter

And you need a IValueConverter in your binding of DataTrigger

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; } }

而XAML是相当简单

And the xaml is quite simple

<TextBox x:Name="TextBox" Text="{Binding Text,UpdateSourceTrigger=PropertyChanged}"> </TextBox>

您需要创建在身后code您datatrigger。

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; }

这里的窍门是, ValueConverter 有一个名为依赖属性 CandidateValueProperty 。而这个属性被绑定到了 CandidateValue 视图模型。因此,文本框的前景将是红色的,当输入值等于视图模式CandidateValue。

The trick here is that the ValueConverter has a dependency property named CandidateValueProperty. And this property is bind to the CandidateValue of view model. So the foreground of the TextBox will be red when the input value equals to CandidateValue of view model.

更多推荐

在WPF DataTrigger值绑定

本文发布于:2023-11-06 23:42:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   WPF   DataTrigger

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!