WPF binding DataTrigger value

Well, that might be a simple question, but I can't find a solution for this.

I have a DataTrigger like

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

Now I want to bind Value to the variable myVariable . Therefore, if the value of myVariable changes, the Value DataTrigger property also changes.

I tried to establish a binding, but I think that it is impossible to establish. Is there any other way by which I can set this value dynamically.

EDIT: I tried to create my own data trigger. But I still can't get it to work.

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 instead of the 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> 

Thanks.

+4
source share
1 answer

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

Here is a 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 an IValueConverter in a DataTrigger binding

 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 xaml is pretty simple

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

You need to create your datatrigger in code.

 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 ValueConverter has a dependency property called CandidateValueProperty . And this property is bound to the CandidateValue view model. Thus, the front of the TextBox will be red if the input value is equal to the CandidateValue model.

+4
source

Source: https://habr.com/ru/post/1485181/


All Articles