WPF MVVM DataGrid查看SelectedCellsChanged

时间:2014-11-08 12:47:05

标签: c# wpf xaml mvvm wpfdatagrid

我是WPF MVVM的新手。我想知道如何检测ViewModel中的SelectedCellsChanged事件。有没有办法检测该事件而不将任何代码放入代码隐藏文件。这是我的代码。

MainWindow.xaml

    <Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              
    Title="MainWindow" Height="350" Width="525"
    DataContext="{StaticResource CusVM}">

<Grid>
    <Button x:Name="myButton" Command="{Binding MyButtonClickCommand}" Width="100" Height="50" Content="click" Margin="0,10,417,260" />
    <Label Content="{Binding Name}" Margin="105,37,23,251" />
    <TextBox x:Name="inputBox1" Width="200" Height="30" Margin="22,74,295,216" Text="{Binding Text1, UpdateSourceTrigger=PropertyChanged}"  />
    <TextBox Width="200" Height="30" Margin="263,74,54,216"   />
    <ComboBox HorizontalAlignment="Left" Margin="122,10,0,0" VerticalAlignment="Top" Width="236" ItemsSource="{Binding Addresses}" SelectedItem="{Binding SelectedAddress}" DisplayMemberPath="AddressLine1"  >

    </ComboBox>
    <DataGrid Margin="0,109,0,10" ItemsSource="{Binding Addresses}"/>
</Grid>

查看模型:CustomerViewModel

    namespace WpfApplication1.ViewModels
{
    public class CustomerViewModel : EventBase
    {
    public ICommand MyButtonClickCommand
    {
        get { return new DelegateCommand(FuncToCall, FuncToEvaluate); }
    }

    private Address selected_address;

    public Address SelectedAddress
    {
        get { return selected_address; }
        set { selected_address = value; OnPropertyChanged("SelectedAddress"); Name = value.AddressLine1; }
    }

    IEnumerable<Address> addresses = new List<Address>();

    public IEnumerable<Address> Addresses
    {
        get { return addresses; }
        set 
        { 
            addresses = value;
            OnPropertyChanged("Addresses");

        }
    }
    public CustomerViewModel()
    {
        fillList();
    }
    private void fillList()
    {
        List<Address> addr = new List<Address>();
        addr.Add(new Address() { AddressID=1, AddressLine1="test1"});
        addr.Add(new Address() { AddressID=2, AddressLine1="test2"});
        addr.Add(new Address() { AddressID = 3, AddressLine1 = "test3" });
        addresses = addr;
    }


    private string text1;

    public string Text1
    {
        get { return text1; }
        set { 
            text1 = value;
            OnPropertyChanged("Text1");
            Name = text1;
        }
    }



    private string name;

    public string Name
    {
        get { return name; }
        set { 
            name = value;
            OnPropertyChanged("Name");
        }
    }

    private void FuncToCall(object context)
    {
        Name = "test result";
    }

    private bool FuncToEvaluate(object context)
    {

        return true;
    }


}
}

1 个答案:

答案 0 :(得分:0)

我想你可能会找到答案here。我会把它添加为评论,但我还没有足够的代表。