简体   繁体   English

在将(视图的)背后代码中的依赖属性绑定到其视图模型的属性时,我在做错什么?

[英]What am I doing wrong in this binding of a dependency property in the code behind (of a view) to a property its viewmodel?

A couple of other posts seem to indicate that data can be shared between a View's code behind and viewmodel by binding the dependency property in the code behind and property in the viemodel. 其他几篇文章似乎表明,通过绑定背后代码中的dependency属性和viemodel中的属性,可以在View的背后代码和viewmodel之间共享数据。 Also, I have read that the DP should be in the code behind when itself is being bound in a Main Window/User Control relationship. 另外,我已经读到,当DP被绑定在主窗口/用户控件关系中时,它应该位于后面的代码中。

The following is from the code behind (SetupUC) 以下是来自(SetupUC)背后的代码

 public static readonly DependencyProperty UC1Property =
     DependencyProperty.Register(
     "UC1", typeof(string), typeof(SetupUC),
     new FrameworkPropertyMetadata()
     {
        PropertyChangedCallback = OnUC1Changed,
        BindsTwoWayByDefault = true
     });

    public string UC1
    {
        get { return (string)GetValue(UC1Property); }
        set
        {
            SetValue(UC1Property, value);
        }
    }

public SetupUC()
    {
        InitializeComponent();
        SetupViewModel svm = new SetupViewModel();
        this.DataContext = svm;
        Binding binding = new Binding("ViewModelStringProperty") { Source = svm, Mode = BindingMode.TwoWay };
        BindingOperations.SetBinding(this, SetupUC.UC1Property, binding);
    }

and the viewmodel (SetupViewModel) 和viewmodel(SetupViewModel)

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

public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

In debugging, UC1 seems to always be updated correctly from the Main Window as its changes are reflected in the user control view. 在调试中,UC1似乎总是从主窗口正确更新,因为它的更改反映在用户控件视图中。 However, in the viewmodel, ViewModelStringProperty does not ever seem to be updated - it's always null. 但是,在视图模型中,ViewModelStringProperty似乎从未更新过-它始终为null。 Full disclosure(!), the following is how UC1 is bound in the user control XAML 完全公开(!),以下是UC1在用户控件XAML中的绑定方式

<TextBox x:Name="tbx1" HorizontalAlignment="Left" Height="23" Margin="159,22,0,0" TextWrapping="Wrap" Text="{Binding UC1, ElementName=root}" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.017,0.304"/>

Again, this part seems to be fine, it's getting data to ViewModelStringProperty in the viewmodel that is not. 同样,这部分似乎还不错,它正在将数据获取到不是ViewModel中的ViewModelStringProperty中。

To ensure your changes in your TextBox are immediately pushed to the DependencyProperty and subsequently the view-model, ensure you have UpdateSourceTrigger=PropertyChanged on your TextBox.Text binding, ie: 为了确保您在TextBox的更改立即被推到DependencyProperty以及随后的视图模型中,请确保您在TextBox.Text绑定上具有UpdateSourceTrigger=PropertyChanged ,即:

Text="{Binding UC1, ElementName=root, UpdateSourceTrigger=PropertyChanged}"

With this, my example worked perfectly fine. 这样,我的示例就可以很好地工作了。

If this doesn't help, I suggest adding two additional TextBlocks , one bound to the UC1 and one bound to ViewModelStringProperty , this will make it easier to tell which are getting updated correctly, eg: 如果这样做没有帮助,我建议添加两个额外的TextBlocks ,一个绑定到UC1 ,另一个绑定到ViewModelStringProperty ,这将使您更容易知道哪个正在正确更新,例如:

<StackPanel>
    <TextBox Text="{Binding UC1, ElementName=Root, UpdateSourceTrigger=PropertyChanged}" />
    <TextBlock Text="{Binding UC1, ElementName=Root}" />
    <TextBlock Text="{Binding ViewModelStringProperty}" />
</StackPanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM