在代码中绑定属性

时间:2012-08-16 12:23:01

标签: c# .net wpf data-binding dependency-properties

我正在尝试在代码中设置属性绑定(WPF)。代码编译很好,但我绑定的属性永远不会设置。下面是一个最小的例子:

视图模型:

public class FooViewModel: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _value;
    public string Value 
    { 
        get { return _value; }
        set
        {
            _value = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Value"));
        }
    }
}

观点:

public class FooView: Window
{
    public string Value
    {
        get { return Title; }
        set
        {
            // Breakpoint here never hits!
            Title = value;
        }
    }

    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(FooView));

    public FooView()
    {
        Binding valueBinding = new Binding("Value");
        valueBinding.Mode = BindingMode.OneWay;
        SetBinding(ValueProperty, valueBinding);
    }
}

“main()”:

        FooView view = new FooView();
        FooViewModel model = new FooViewModel();
        view.DataContext = model;
        view.Show();
        model.Value = "ABC";

我希望在FooView.Value设置时调用model.Value - setter。我也尝试过明确地将Binding.Source属性设置为模型。如何设置绑定?

3 个答案:

答案 0 :(得分:3)

这里的问题是Value属性与ValueProperty依赖属性完全无关。

依赖项属性的CLR包装器需要调用DependencyObject的GetValueSetValue方法,如下所示:

public string Value
{
    get { return (string)GetValue(ValueProperty); }
    set { SetValue(ValueProperty, value); }
}

要获得有关房产更改的通知,您必须使用PropertyChangedCallback注册PropertyMetadata

public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register(
        "Value", typeof(string), typeof(FooView),
        new PropertyMetadata(ValuePropertyChanged));

private static ValuePropertyChanged(DependencyObject obj,
    DependencyPropertyChangedEventArgs e)
{
    // obj is your FooView instance
    // get new property value from e.NewValue
}

Custom Dependency Properties中获取更多信息。

答案 1 :(得分:1)

我认为你错过了Source属性:

Binding myBinding = new Binding("Value");
myBinding.Source = TheSourceOfTheProprty;
myBinding.Mode = BindingMode.OneWay;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
myBinding.IsAsync = false;

BindingOperations.SetBinding(YourControl, YourControl.Property, myBinding);

修好你的Dependency Property像Clemens解释的那样,你只需要设置你的绑定就可以了。你好了。

所以你的绑定看起来像这样:

FooViewModel model = new FooViewModel();

FooView view = new FooView(model);
view.DataContext = model;
view.Show();
model.Value = "ABC";


public FooView(FooViewModel model)
{
    Binding myBinding = new Binding("Value");
    myBinding.Source = model;
    myBinding.Mode = BindingMode.OneWay;
    myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

    BindingOperations.SetBinding(this, this.Value, myBinding);
}

答案 2 :(得分:0)

如果我没有错,你将要从后面的代码设置你的fooview窗口的title属性。如果是这样,那么你可以在你的fooview构造函数

下使用xe绑定行
 Binding valueBinding = new Binding("Value");
valueBinding.Mode = BindingMode.TwoWay;
SetBinding(TitleProperty, valueBinding);|