Reputation: 4554
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
Reputation: 13983
You have two solutions.
IsSelected
property. Then you should adjust bindings and
notify parent VM to update its property SelectedItem
or
SelectedItems
SelectedItems
to work in both directions (VM -> V and V
-> VM) without the need to declare IsSelected
property for the items.ICommand
. And this command is executed every time selection is
changed by user actions. The parameter of the command is
DataGrid.SelectedItems collection.If you need I can share the code when come to the office tomorrow.
Upvotes: 0
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
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