1

I am working on a WPF application. As per requirement I have a dynamic data grid (User can select the columns to display) in my main page. Currently I am using MVVM design pattern. One of the column in my data grid is “Line Number”(1,2,3,…250) . As per new requirement I need to add a text box in this main screen. Once the user enter the line number in this test box and clicking “ENTER” key I need to select/Focus the corresponding line in the datagrid. How can I implement this from Vie Model?

  1. How can I select a data grid Row from the View Model.?

  2. How can I move the scroll position to that particular data row?

Any help would be appreciable.

Thanks

Ranish
  • 877
  • 2
  • 10
  • 30

1 Answers1

1

First, each item of the collection should have a Boolean property to indicate whether it's selected (let's call it Selected). Then in data grid:

<DataGrid>
    <DataGrid.ItemContainerStyle>
        <Style TargetType="{x:Type DataGridRow}">
             <Setter Property="IsSelected" Value="{Binding Selected, Mode=TwoWay}"/>
        </Style>
    <DataGrid.ItemContainerStyle>
 ........
 </DataGrid>

[of course all properties should have change notification]

About scrolling, I'd go with a this answer, the question is targeting ListBox but the answer is for ItemsControl in general, so it should work for DataGrid.

So, when the user enters line number, you find the relevant item, set it's Selected property to true and everything else should be taken care of.

Community
  • 1
  • 1
XAMeLi
  • 6,189
  • 2
  • 22
  • 29