简体   繁体   中英

WPF MVVM DataGrid View SelectedCellsChanged

I'm new to the WPF MVVM. I want to know about how to detect SelectedCellsChanged event inside my ViewModel. Is there any way to detect that event without putting any code into the code behind file. This is my code.

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>

View Model : 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;
    }


}
}

I think you might find the answer here . I would've add it as a comment, but I don't have enough rep just yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM