2014-11-08 189 views
0

我是新来的WPF MVVM。我想知道如何检测ViewModel中的SelectedCellsChanged事件。没有任何方法可以在没有将任何代码放入代码背后的代码的情况下检测该事件。这是我的代码。WPF MVVM DataGrid查看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; 
    } 


} 
} 
+0

如果你可以用,当你选择一个新的行知住,只需添加“的SelectedItem =“{结合SelectedAddress,模式=双向}”到您的DataGrid,然后你可以做任何你想在set访问的SelectedAddress财产 – 2014-11-08 13:54:53

回答

0

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