Prageeth godage
Prageeth godage

Reputation: 4554

MVVM WPF Datagrid SelectedValue bind by view model?

Anyone knows how to change the SelectedValue of the datagrid by ViewModel.If we change View then It will fire VM but not vise versa.

Upvotes: 2

Views: 3415

Answers (3)

Pavel Voronin
Pavel Voronin

Reputation: 13983

You have two solutions.

  • Each VM which stands as an item of the DataGrids's collection can implement IsSelected property. Then you should adjust bindings and notify parent VM to update its property SelectedItem or SelectedItems
  • We wanted SelectedItems to work in both directions (VM -> V and V -> VM) without the need to declare IsSelected property for the items.
    I've coded static class SelectionHelper with several attached properties. One of these properties accepts collection of items and keeps DataGrid.SelectedItems in sync with it. Thus we can contol selections from the VM.
    Another property is of type ICommand. And this command is executed every time selection is changed by user actions. The parameter of the command is DataGrid.SelectedItems collection.
    Now I planned the support of direct manipulation of the bound collection instead of executing the command.

If you need I can share the code when come to the office tomorrow.

Upvotes: 0

yo chauhan
yo chauhan

Reputation: 12295

 public ViewModel()
    {
        PriceLogs = new ObservableCollection<PriceLog>();

        PriceLogs.Add(new PriceLog() { LogDateTime = DateTime.Now.AddDays(2), Price = 200 });
        PriceLogs.Add(new PriceLog() { LogDateTime = DateTime.Now.AddDays(20), Price = 200 });
        PriceLogs.Add(new PriceLog() { LogDateTime = DateTime.Now.AddDays(50), Price = 200 });
        PriceLogs.Add(new PriceLog() { LogDateTime = DateTime.Now.AddDays(80), Price = 600 });
        PriceLogs.Add(new PriceLog() { LogDateTime = DateTime.Now.AddDays(80), Price = 300 });
        PriceLogs.Add(new PriceLog() { LogDateTime = DateTime.Now.AddDays(2), Price = 200 });
        //Here is how you can change selected Item from ViewModel
        SelectedPriceProlog = PriceLogs.Last();
       // SelectedPriceProlog = PriceLogs[2];
  }
    public ObservableCollection<PriceLog> PriceLogs { get; set; }

    private PriceLog selectedPriceProlog;
    public PriceLog SelectedPriceProlog 
    {
        get { return selectedPriceProlog; }

        set
        {
            selectedPriceProlog = value;
            Notify("SelectedPriceProlog");
        }
    }

<DataGrid ItemsSource="{Binding PriceLogs}" SelectedItem="{Binding SelectedPriceProlog, Mode=TwoWay}">

How to set Selected Value

    public ViewModel()
    {
        PriceLogs = new ObservableCollection<PriceLog>();

        PriceLogs.Add(new PriceLog() { LogDateTime = DateTime.Now.AddDays(2), Price = 200 });
        PriceLogs.Add(new PriceLog() { LogDateTime = DateTime.Now.AddDays(20), Price = 200 });
        PriceLogs.Add(new PriceLog() { LogDateTime = DateTime.Now.AddDays(50), Price = 200 });
        PriceLogs.Add(new PriceLog() { LogDateTime = DateTime.Now.AddDays(80), Price = 600 });
        PriceLogs.Add(new PriceLog() { LogDateTime = DateTime.Now.AddDays(80), Price = 900 });
        PriceLogs.Add(new PriceLog() { LogDateTime = DateTime.Now.AddDays(2), Price = 200 });
        //Here is how you can change selected value from ViewModel
        SelectedPrice = 900;
       // SelectedPriceProlog = PriceLogs[2];

        //Or ypu can set 
  }
    public ObservableCollection<PriceLog> PriceLogs { get; set; }


    private int selectedPrice ;
    public int SelectedPrice 
    {
        get { return SelectedPrice ; }

        set
        {
            selectedPrice = value;
            Notify("SelectedPriceProlog");
        }
    }

 <DataGrid ItemsSource="{Binding PriceLogs}" SelectedValue="{Binding SelectedPrice, Mode=TwoWay}" SelectedValuePath="Price">

You can do it by binding SelectedItem property of DataGrid to ViewModel Property that must be of type that your DataGrids ItemSource and binding must be TwoWay and then you can set that property in VewModel to any of the item of your collection.Or you can do it with SelectedValue as i shown above .Now if you want to change from View to ViewModel Only then your binding Mode must be OneWay .I hope this will help.

Upvotes: 2

HichemSeeSharp
HichemSeeSharp

Reputation: 3318

You are looking for a DataBinding that updates the ViewModel but not vise versa so, there is a DataBinding mode called OneWayToSource for ex :
<TextBox Text="{Binding TextProperty, Mode=OneWayToSource}"/>

Upvotes: 0

Related Questions