[英]wpf binding not updating
Using MVVM, I have a View that contains a DependencyProperty, and a ListBox that is bound to the ViewModel: 使用MVVM,我有一个包含DependencyProperty的视图和一个绑定到ViewModel的列表框:
public static readonly DependencyProperty SelectedServerProperty =
DependencyProperty.Register("SelectedServer",typeof(object),
typeof(ServerView), new FrameworkPropertyMetadata(test));
public object SelectedServer
{
get { return GetValue(SelectedServerProperty); }
set { SetValue(SelectedServerProperty, value); }
}
public ServerView()
{
SetBinding(SelectedServerProperty, "SelectedServer");
InitializeComponent();
}
public static void test(DependancyObject sender, DependancyPropertyChangedEventArgs e)
{
...
}
XAML: XAML:
<ListBox ItemsSource="{Binding Servers}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedServer}" />
and a ViewModel that implements INotifyPropertyChanged: 和一个实现INotifyPropertyChanged的ViewModel:
ServerModel _selectedServer;
public ServerModel SelectedServer
{
get { return _selectedServer; }
set
{
_selectedServer = value;
RaisePropertyChanged("SelectedServer");
}
}
When i select an item in the list, the ViewModel is updated, but the DependencyProperty in the View stays the same. 当我在列表中选择一个项目时,ViewModel会更新,但是View中的DependencyProperty保持不变。 even the test event i created is not triggered. 即使我创建的测试事件也不会触发。 What am I doing wrong? 我究竟做错了什么?
I think the SetBinding
is trying to bind to a Property
defined in the View and not VM. 我认为SetBinding
试图绑定到View中定义的Property
,而不是VM。 So try using the syntax 因此,请尝试使用语法
SetBinding(SelectedServerProperty, new Binding("SelectedServer"));
This would try to bind to the SelectedServer
property in the VM ( DataContext
of the View). 这将尝试绑定到VM(视图的DataContext
)中的SelectedServer
属性。 Here is the link from MSDN which would help you. 这是来自MSDN的链接,它将为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.