MVVM viewmodel事件(命令?)

时间:2012-02-22 02:18:50

标签: c# wpf mvvm itemssource

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

这是我的MVVM代码:

主窗口

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>

1 个答案:

答案 0 :(得分:0)

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

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

示例:

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

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中的属性。