WPF数据绑定问题 [英] WPF DataBinding Issue
本文介绍了WPF数据绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的窗口中,我试图将控件绑定到表单中的属性.我似乎无法使其按预期工作.在后面的代码中,我有以下定义:
In my window, I''m trying to bind a control to a property in the form. I can''t seem to get it to work as intended. In the code-behind I have this defined:
public AViewModel ViewModel { get; set; }
并且在XAML中我有这个(我跳过了不必要的xaml标记,以简化问题):
and in the XAML I have this (I skipped the unneccesary xaml tags and such to simplify the question):
TextBlock Text="{Binding Path=ViewModel.SomeProperty}"
但是它的确在窗口中显示了边界值.
我还尝试在xaml中定义一个本地"名称空间,并将"local:"放在绑定语句的前面.
我到底在做什么错?
but it does display the bound value in the window.
I also tried defining a "local" namespace in the xaml and putting "local:" in front of the binding statement.
What the hell am I doing wrong?
推荐答案
您必须使用属性ViewModel设置窗口DataContext,然后它将起作用.
You must set window DataContext with property ViewModel, then it will be working.
public AViewModel ViewModel
{
get{ return m_viewModel; }
set
{
m_viewModel = value;
DataContext = m_viewModel;
}
}
这篇关于WPF数据绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文