我有帶有顯示通知屬性的 ViewModel:
public class NotificationViewModel : BaseViewModel
{
private string errorText;
public string ErrorText
{
get => this.errorText;
set
{
this.errorText = value;
this.OnPropertyChanged();
}
}
}
然后在 MainViewModel.cs 我有以下命令,如果出現錯誤,應該顯示通知:
public NotificationViewModel NotificationViewModel { get; private set; }
public MainViewModel()
{
}
private async Task AddProjectToDatabase()
{
if (!string.IsNullOrEmpty(this.ProjectNumber))
{
// some other code here
}
else
{
NotificationDialog view = new NotificationDialog
{
DataContext = new NotificationViewModel()
};
this.NotificationViewModel.ErrorText = "Select project number first!";
object result = await DialogHost.Show(view, "MainDialogHost", this.ExtendedOpenedEventHandler, this.ExtendedNotificationClosingEventHandler);
}
}
我試圖:
public MainViewModel()
{
this.NotificationViewModel = new NotificationViewModel();
}
但是在這種情況下,我沒有顯示任何文本,因為現在初始化了 NotificationViewModel 的新實體。我可能必須以某種方式將現有實體注入 MainViewModel.cs ?我怎么能這樣做?如果有什么遺漏可以提供答案,請發表評論。
uj5u.com熱心網友回復:
您可以將NotificationViewModel屬性設定為DataContextof NotificationDialog。
private async Task AddProjectToDatabase()
{
if (!string.IsNullOrEmpty(this.ProjectNumber))
{
// some other code here
}
else
{
NotificationDialog view = new NotificationDialog
{
DataContext = this.NotificationViewModel
};
this.NotificationViewModel.ErrorText = "Select project number first!";
object result = await DialogHost.Show(view, "MainDialogHost", this.ExtendedOpenedEventHandler, this.ExtendedNotificationClosingEventHandler);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424231.html
上一篇:帶有XAML的Powershell:處理ListBox資料模板中按鈕的單擊事件
下一篇:將文本框系結到按鈕(MVVM)
