MVVM 视图模型事件(命令?)

发布于 2025-01-08 01:01:00 字数 3507 浏览 3 评论 0原文

我有一个 MVVM 设置,可以在我的主窗口上创建一个视图。我不确定如何知道用户何时单击视图内的特定通知项目。我应该在哪里添加事件或知道事件何时发生的命令?

这是我的 MVVM 代码:

:

xaml

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

:

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

NotificationModelNotificationViewModelNotificationView

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
}

MainWindow

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);
    }
}

cs

<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>

I have a MVVM setup that creates a View on my MainWindow. I am not sure how to know when a user Clicks on a specific Notification Item inside the View. Where would I add the event, or a command to know when that happens?

here are is my MVVM code :

MainWindow

cs:

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

xaml:

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

NotificationModel

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
}

NotificationViewModel

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);
    }
}

NotificationView

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2025-01-15 01:01:00

ItemsControl 中没有内置真正的选择机制。将 ItemsControl 切换为 ListBox 可能会解决您的问题。

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

示例:

在视图模型的构造函数中:

PropertyChanged += NotificationViewModel_PropertyChanged;

向视图模型添加属性以允许绑定:

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

最后,向视图模型添加事件处理程序:

NotificationViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e))
{
    if (e.PropertyName = "SelectedNotification") DoStuff();
}

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

There's no real selection mechanism built into an ItemsControl. It would probably solve your problem to switch out your ItemsControl for a ListBox.

If you do that, you can bind to SelectedItem, then handle any changes made to SelectedItem using the PropertyChanged event.

Example:

In your view model's constructor:

PropertyChanged += NotificationViewModel_PropertyChanged;

Add a property to your view model to allow the binding:

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

Finally, add the event handler to your view model:

NotificationViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e))
{
    if (e.PropertyName = "SelectedNotification") DoStuff();
}

You may find that you don't even need to hook into PropertyChanged if you just want to update another control in your view based on the selected item in your list box. You can just bind directly to the property within xaml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文