I have SelectedDataItem bound to an object in my ViewModel. I can find items in my list and update them, but the item I am updating does not display in the grid as it is outside teh scrolling region.
i want to be able to find an item in the list, udpate it and have that item displayed in the scrollable portion of the screen.
I need to do this from my vie wmodel, not from code behind
Can anyone help - this seems liek such a simple thing to do
Hello,
There is no need to have the logic in the code behind, as when you pass a command parameter to your command, you would be able to do the same from your ViewModel, which is the better approach.
However, when passing the parameter, you will have to reference it, otherwise it will be null:
Command="{Binding bringCellIntoView}" CommandParameter="{x:Reference xamDataGrid1}"
Then initialize the command in the ViewModel"
public ViewModel() {
bringCellIntoView = new RelayCommand(bringDataItemIntoView);
}
and now the method in your ViewModel can look like this:
public void bringDataItemIntoView(object parameter) { var grid = parameter as XamDataGrid; grid.SelectedDataItem = this.dataItem grid.BringDataItemIntoView(this.dataItem, false); }
I have attached a C# project for reference, as you have stated this would be fine.
Please let me know if you have further questions.
Sincerely,Tihomir TonevAssociate Software DeveloperInfragistics
XDG_MVVM_Button.zip
I played around and ended up with a solution. I changed the button to have a click method in the code behind, and in that I call into the View model to call the serach method, and then do the following to scroll teh selected record into view. Is this OK or is there a better way to do it ?
Note my example is in synergy.net code
Here is my simplified searchcommand in the view model. It is written in synergy.net., but as you can see it is very simple.
I am happy for you to give me the solution in C#
I am passing the name of the Xamdatagrid in as param.
I have a an order with 134 detail lines on it, displayed in a Xamdatagrid. When they click the button the view model picks an item near the end of the list, which is off screen. I want to be able to srcoll the list to the selected item. I also want to see new items I add to the end of the list, but that is just the same thing.
Here is my XAML Code for the button and teh Xamdatagrid
Note that the required record is selected and the data is displayed correctly in a data editing user control. The only thing I don;t know how to do is scroll the grid to bring the selected record into view
SelectedDataItem will select a record, but won't scroll your grid to this item. If you would like the DataItem to be also displayed, you can call the BringDataItemIntoView() method, which will do the trick.
Since you are doing this from your ViewModel, I suppose you have a command to search in the grid and you can pass the grid as a command parameter into this binding, so you can call the method.
In case you need help implementing this, please provide me with more information of the scenario, and I will prepare a sample for you.