0

我正在尝试在代码中设置属性绑定(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);
    }
}

主要的()”:

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

我希望在设置FooView.Value时调用 -setter model.Value。我还尝试将Binding.Source属性显式设置为模型。绑定应该如何设置?

4

3 回答 3

3

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

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

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

为了获得有关属性更改的通知,您必须使用PropertyMetadata注册一个PropertyChangedCallback

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
}

在自定义依赖属性中获取更多信息。

于 2012-08-16T12:42:43.027 回答
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);
}
于 2012-08-16T12:26:56.310 回答
0

如果我没记错的话,您将从后面的代码中设置您的 fooview 窗口的标题属性。如果是这样,那么您可以在 fooview 构造函数下使用 xe 绑定行

 Binding valueBinding = new Binding("Value");
valueBinding.Mode = BindingMode.TwoWay;
SetBinding(TitleProperty, valueBinding);|
于 2012-08-16T12:58:42.287 回答