将嵌套的 UserComponent 绑定到它的 ViewModel
我有一个 WFP 项目,并且正在使用 MVVM 模式。 我有在 CustomerView UserControl 中使用的 AddressView User 控件。
我的 AddressVeiw userControl 有一个 AddressViewModel,CustomerView 有一个 CustomerViewModel
CustomerViewModel 的代码
public DelegateCommand
AddressViewModel 的代码
private ObservableCollection
; CountryList = new ObservableCollection (); 公共 ObservableCollection ;国家列表 { 获取{返回国家列表; } 设置{国家/地区列表=值; } } 公共 DelegateCommand<对象>;保存命令 { 获取;放; } 私有无效负载() { 尝试 { CountryList = (new CountryRepository().GetAll()); } catch(异常前) { OnSetStatusBarText("错误:" + ex.Message.ToString()); } } 私人无效OnSetStatusBarText(字符串消息) { var evt = eventAgg.GetEvent (); evt.Publish(消息); } 私有无效InitializeCommands() { SaveCommand = new DelegateCommand (OnSaveCommand, CanSaveExcute); } 私有 bool CanSaveExcute(对象 obj) { 返回真; } 私有无效OnSaveCommand(对象obj) { }
一些我如何将我的 AddressViewModel 挂接到我的 AddressView,客户工作正常...... 必须做什么才能解决这个问题? 谢谢
I have a WFP Project and i am using MVVM Pattern.
I have AddressView User control which i used in CustomerView UserControl.
<my:AddressVeiw Width="340" DataContext="AddressViewModel"/>
My AddressVeiw userControl has a AddressViewModel and CustomerView has a CustomerViewModel
Code for CustomerViewModel
public DelegateCommand<object> SaveCommand { get; set; } private string firstName; public string FirstName { get { return firstName; } set { firstName = value; RaisePropertyChanged("FirstName"); SaveCommand.RaiseCanExecuteChanged(); } } private string lastName; public string LastName { get { return lastName; } set { lastName = value; RaisePropertyChanged("LastName"); SaveCommand.RaiseCanExecuteChanged(); } } private AddressViewModel addressViewModel; public AddressViewModel AddressViewModel { get { return addressViewModel; } set { addressViewModel = value; } } private string middleName; public string Middlename { get { return middleName; } set { middleName = value; RaisePropertyChanged("MiddleName"); SaveCommand.RaiseCanExecuteChanged(); } } private string fullName; public string FullName { get { return fullName; } set { fullName = value; RaisePropertyChanged("FullName"); } } private void InitializeCommands() { SaveCommand = new DelegateCommand<object>(OnSaveCommand, CanSaveExcute); } private bool CanSaveExcute(object obj) { if (string.IsNullOrEmpty(firstName) ||string.IsNullOrEmpty(lastName)) return false; return true; } private void OnSaveCommand(object obj) { FullName = FirstName + " " + LastName; } }
Code for AddressViewModel
private ObservableCollection<Country> countryList = new ObservableCollection<Country>(); public ObservableCollection<Country> CountryList { get { return countryList; } set { countryList = value; } } public DelegateCommand<object> SaveCommand { get; set; } private void Load() { try { CountryList = (new CountryRepository().GetAll()); } catch (Exception ex) { OnSetStatusBarText("Error: " + ex.Message.ToString()); } } private void OnSetStatusBarText(string message) { var evt = eventAgg.GetEvent<StatusBarMessageEvent>(); evt.Publish(message); } private void InitializeCommands() { SaveCommand = new DelegateCommand<object>(OnSaveCommand, CanSaveExcute); } private bool CanSaveExcute(object obj) { return true; } private void OnSaveCommand(object obj) { }
Some how i can hook my AdddressViewModel To my AddressView, Customer Works fine...
What Must be done to resolve this problem?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要对 AddressView 的 DataContext 使用绑定表达式。而不是这个...
...尝试这个...
You need to use a binding expression for the DataContext of your AddressView. Instead of this...
...try this...
你很接近,但你需要一个绑定:
You're close, but you need a binding: