gpt4 book ai didi

c# - MVVM View 模型事件(命令?)

转载 作者:行者123 更新时间:2023-12-03 10:49:42 25 4
gpt4 key购买 nike

我有一个 MVVM 设置,可以在我的 MainWindow 上创建一个 View 。我不确定如何知道用户何时单击 View 中的特定通知项。我将在哪里添加事件或命令以知道何时发生?

这是我的 MVVM 代码:

主窗口

CS:

NotificationViewModel notificationViewModel = new NotificationViewModel();
notificationViewModel.AddNoticiation(new NotificationModel() { Message = "Error", Name = "Station 21" });
NotificationView.DataContext = notificationViewModel;

xml:
<notification:NotificationView x:Name="NotificationView" />

通知模型
public class NotificationModel : INotifyPropertyChanged
{

private string _Message;
public string Message
{
get { return _Message; }
set
{
if (_Message != value)
{
_Message = value;
RaisePropertyChanged("Message");
}
}
}

private string _Name;
public string Name
{
get { return _Name; }
set
{
if (_Name != value)
{
_Name = value;
RaisePropertyChanged("Name");
}
}
}

public string TimeStamp
{
get { return DateTime.Now.ToString("h:mm:ss"); }
}

#region PropertChanged Block
public event PropertyChangedEventHandler PropertyChanged;

private void RaisePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
#endregion
}

通知 View 模型
public class NotificationViewModel 
{
private ObservableCollection<NotificationModel> _Notifications = new ObservableCollection<NotificationModel>();
public ObservableCollection<NotificationModel> Notifications
{
get { return _Notifications; }
set { _Notifications = value; }
}

public void AddNoticiation(NotificationModel notification)
{
this.Notifications.Insert(0, notification);
}
}

通知 View
<Grid>
<StackPanel HorizontalAlignment="Left" >
<ItemsControl ItemsSource="{Binding Path=Notifications}"
Padding="5,5,5,5">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Background="SlateGray"
CornerRadius="4">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Text="{Binding Path=TimeStamp}" />
<TextBlock Grid.Column="1"
Text="{Binding Path=Name}" />
<TextBlock Grid.Column="2"
Text="{Binding Path=Message}" />
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>

最佳答案

ItemsControl 中没有内置真正的选择机制。 .关闭 ItemsControl 可能会解决您的问题对于 ListBox .

如果你这样做,你可以绑定(bind)到 SelectedItem ,然后处理对 SelectedItem 所做的任何更改使用 PropertyChanged事件。

例子:

在您的 View 模型的构造函数中:

PropertyChanged += NotificationViewModel_PropertyChanged;

向 View 模型添加一个属性以允许绑定(bind):
private string _selectedNotification;
public string SelectedNotification
{
get { return _selectedNotification; }
set
{
if (_selectedNotification != value)
{
_selectedNotification = value;
RaisePropertyChanged("SelectedNotification");
}
}
}

最后,将事件处理程序添加到您的 View 模型:
NotificationViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e))
{
if (e.PropertyName = "SelectedNotification") DoStuff();
}

你可能会发现你甚至不需要 Hook PropertyChanged如果您只想根据列表框中的选定项目更新 View 中的另一个控件。您可以直接绑定(bind)到 xaml 中的属性。

关于c# - MVVM View 模型事件(命令?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9388258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com