如果在构造函数中设置DataContext,在调用InitializeComponent()之前还是之后设置它是否无关紧要? [英] If setting a DataContext within a constructor, does it matter if I set it before or after the call to InitializeComponent()?

查看:55
本文介绍了如果在构造函数中设置DataContext,在调用InitializeComponent()之前还是之后设置它是否无关紧要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF窗口,该窗口在其构造函数中带有一些参数.然后,我使用这些构造函数来设置窗口的状态.构造函数过程的一部分是实例化我的视图模型类,然后将其设置为Windows DataContext .

I have a WPF window that takes a few parameters in it's constructor. I then use these constructors to setup the state of the window. Part of that constructor process is instantiating my view model class that is then set as the windows DataContext.

我的问题是,何时应在调用 InitializeComponent()之前或之后将 DataContext 设置为与视图模型对象相等?

My question is when should I set my DataContext equal to my view model object-- before or after the call to InitializeComponent()?

我问是因为如果我事先设置了它,那么我需要手动启动在初始化窗口后之后执行的代码,因为某些事件应在 DataContext 时触发>已分配或重新分配.

I ask because if I set it beforehand, I then need to manually launch code that is executed after the window has been initialized, because certain events should fire when the DataContext is assigned, or re-assigned.

我假设如果在调用 InitializeComponent()之后设置 DataContext ,应该不会有任何绑定问题,但是我想征询有关的建议最终致电以这种方式连接我的窗户之前,请先解决此问题.如果在调用 InitializeComponent()之后设置了我的 DataContext ,我会丢失一些可能困扰我的东西吗?

It is my assumption that there shouldn't be any binding issues if I set the DataContext after the call to InitializeComponent() but I wanted to ask for advice on the matter before making the final call to wire up my window this way. Could I be missing something the could come back to haunt me if I set my DataContext after the call to InitializeComponent()?

推荐答案

我的问题是,何时应在调用InitializeComponent()之前或之后将DataContext设置为与视图模型对象相等?

My question is when should I set my DataContext equal to my view model object-- before or after the call to InitializeComponent()?

没关系,除非您依赖于在InitializeComponent()调用期间建立的某些绑定,例如 ElementName 绑定:

It shouldn't matter unless you rely on some bindings that are established during the call to the InitializeComponent(), like ElementName bindings:

无法将ItemsSource绑定到ElementName

InitializeComponent()方法本身可以找到已编译XAML文件的URI,并将其传递给解析BAML的 LoadComponent()方法,即已编译XAML,并创建您在XAML标记中定义的元素的实例:

The InitializeComponent() method itself locates a URI to the compiled XAML file and passes it to a LoadComponent() method that parses the BAML, i.e. the compiled XAML, and creates instances of the elements that you have defined in your XAML markup:

什么是.xaml和.xaml.cs文件

只需在调用 InitializeComponent()<之后,将窗口的 DataContext 属性设置为绑定到视图中元素的视图模型类的实例即可/code>方法.无论如何,在构造函数返回之前,不会解析这些绑定.

Simply setting the DataContext property of the window to an instance of a view model class that elements in the view bind to might as well be done after the call to the InitializeComponent() method. These bindings aren't resolved before the constructor returns anyway.

这篇关于如果在构造函数中设置DataContext,在调用InitializeComponent()之前还是之后设置它是否无关紧要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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