2014-11-08 122 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。我會添加它作爲評論,但我還沒有足夠的代表。