UserControl DataContext绑定 [英] UserControl DataContext Binding

查看:364
本文介绍了UserControl DataContext绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案中有三个项目:

I have three projects in my solution:


  • 我的主WPF应用程序,其中包含 MainWindow + MainViewModel

  • 带有UserControl的UserControl库( ConfigEditorView

  • 带有ViewModel的UIProcess类UserControl( ConfigEditorViewModel

  • My main WPF Application which contains a MainWindow + MainViewModel
  • UserControl Library with a UserControl (ConfigEditorView)
  • UIProcess class with the ViewModel for the UserControl (ConfigEditorViewModel)

在我的MainWindow中,我想将UserControl与UIProcess的ViewModel一起使用。

In my MainWindow I want to use the UserControl with the ViewModel of UIProcess.

首先,在我的 MainWindow 中设置UserControl:

First I set the UserControl in my MainWindow:

<TabItem Header="Editor">
   <Grid>
     <cel:ConfigEditorView DataContext="{Binding ConfEditModel, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
   </Grid>
</TabItem>

我不知道在这里需要这些属性中的哪一个,因此我将它们放在一起,但仍然

I don't know which of these properties I need here, so I put all together but it still doesn't work.

然后在我的 MainViewModel 中设置它:

public ConfigEditorViewModel ConfEditModel { get; set; }

使用绑定到Button的简单方法:

With simple method that is bound to a Button:

private void doSomething()
{
    ConfEditModel = new ConfigEditorViewModel("Hello World");
}

我的 ConfigEditorViewModel 基本上是这样的:

public class ConfigEditorViewModel : ViewModelBase
{
    private string _Description;
    public string Description
    {
        get
        {
            return _Description;
        }
        set
        {
            _Description = value;
            base.RaisePropertyChanged();
        }
    }

    public ConfigEditorViewModel(string t)
    {
        Description = t;
    }
}

此说明已绑定到我的UserControl中的TextBox。

The description is bound to a TextBox in my UserControl.

<TextBox Grid.Row="1" Grid.Column="1" Margin="0,0,0,10" Text="{Binding Description}"/>

启动应用程序并单击按钮时,文本框应包含 Hello World,但为空。

When I start the application and click the Button the TextBox should contain "Hello World" but it's empty.

我做错了什么?

推荐答案

您的视图模型(以及可选的模型)需要实现 INotifyPropertyChanged

Your view models (and, optionally, models) need to implement INotifyPropertyChanged.

绑定并不是魔术。没有内置的机制可以在普通的旧属性的值发生更改时通知代码。您必须对其进行轮询以检查是否发生了更改,这在性能方面会非常糟糕。

Binding's aren't magic. There is no inbuilt mechanism that allows for code to be notified when a plain old property's value changes. You'd have to poll it in order to check to see if a change happened, which would be very bad, performance-wise.

因此,绑定将查看对象它们绑定在一起,并查看它们是否实现INotifyPropertyChanged,如果是,则将订阅PropertyChanged事件。这样,当您更改属性并触发事件时,将通知绑定并更新UI。

So bindings will look at the objects they are bound against and see if they implement INotifyPropertyChanged and, if so, will subscribe to the PropertyChanged event. That way, when you change a property and fire the event, the binding is notified and updates the UI.

请注意,您必须实现接口并正确使用它。 此示例说它适用于2010年,但效果很好。

Be warned, you must implement the interface and use it correctly. This example says it's for 2010, but it works fine.

这篇关于UserControl DataContext绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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