正从视图模型的财产

我有这样的视图模型:

public class BaseViewModelTech : INotifyPropertyChanged { static string _TechnicianID; public string TechnicianID { get { return _TechnicianID; } set { _TechnicianID = TechnicianID; OnPropertyChanged("TechnicianID"); } } static string _DeviceID; public string DeviceID { get { return _DeviceID; } set { _DeviceID = DeviceID; OnPropertyChanged("DeviceID"); } } // In ViewModelBase.cs public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { this.VerifyPropertyName(propertyName); PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { var e = new PropertyChangedEventArgs(propertyName); handler(this, e); } } [Conditional("DEBUG")] [DebuggerStepThrough] public void VerifyPropertyName(string propertyName) { // Verify that the property name matches a real, // public, instance property on this object. if (TypeDescriptor.GetProperties(this)[propertyName] == null) { string msg = "Invalid property name: " + propertyName; Debug.Fail(msg); } } }

我把它作为参数传递给我的xaml.cs

public partial class BaseView : Window{ BaseViewModelTech viewModel; public BaseView (BaseViewModelTech vm) { InitializeComponent(); viewModel = vm; }}

做什么我写访问它使用绑定throught XAML? 我不明白多个例子。

--------------解决方案-------------

稍微更改您的视图背后的代码:

public partial class BaseView : Window
{
BaseViewModelTech viewModel;

public BaseView (BaseViewModelTech vm)
{
InitializeComponent();
viewModel = vm;
this.DataContext = vm; // <----------- add this
}
}

然后在你的XAML可以有这样的事情:

<TextBlock Text="{Binding TechnicianID}" />

还要注意,在你的制定者所需的属性值改变 ,做了通知,而不是之前:

set
{
_DeviceID = DeviceID;
OnPropertyChanged("DeviceID"); // <------ this goes after the member variable change
}

你的情况,你不能直接直接引用您的视图模型为XAML因您的虚拟机查看实例作为成员。 所以,你应该先设置视图的DataContext的代码隐藏:

public partial class BaseView : Window{
BaseViewModelTech viewModel;
public BaseView (BaseViewModelTech vm)
{
InitializeComponent();
viewModel = vm;
this.DataContext=viewModel;
}}

然后在我的xaml.xaml例如,用于标签:

<Label Content="{Binding TechnicianID }"/>

分类:C# 时间:2015-03-15 人气:0
本文关键词: WPF,数据绑定
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.596 (s). 10 q(s)