在WPF DataTrigger值绑定 [英] Binding in WPF DataTrigger value

查看:1804
本文介绍了在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值绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆