Joshscorp
Joshscorp

Reputation: 1832

DataGrid row and MVVM

I have a wpf datagrid with many rows, each row has some specific behaviors like selection changed of column 1 combo will filter column 2 combo, and what is selected in row 1 column 1 combo cannot be selected in row 2 column 1 combo, etc...

So I am thinking of having a view model for the main datagrid, and another for each row.

Is that a good MVVM implementation? It is so that I can handle each row's change event effectively.

Question is, how do I create "each row" as a user control view? within the datagrid.

I want to implement something like this:

        <TreeView
        Padding="0,4,12,0">

        <controls:CommandTreeViewItem
            Header="Sales Orders"
            Command="{Binding SelectViewModelCommand}"
            CommandParameter="Sales Orders"/>          

    </TreeView>  

Where instead of a TreeView I want a datagrid, and instead of controls:CommandTreeViewItem a datagrid row in WPF.

Thanks in advance.

Upvotes: 0

Views: 1061

Answers (3)

Tod
Tod

Reputation: 8252

Just create an observable collection of your view model class in a public property on the viewModel for your view. Here's an example

public ObservableCollection<YourViewModelForEachRow> LineItems{ get; private set; }

Then in your view the datagrid xaml will set the ItemsSource property to the LineItems property you created above.

ItemSource="{Binding LineItems"}

This of course assumes the DataContext for the view containing the DataGrid has been set to the ViewModel where you created LineItems. I do this for my views. I still consider myself a novice at WPF but this seems to be a very clean and flexible approach to MVVM. All the logic for handling data entry, widget handling etc on every row is taken care of by the row view model.

Plus your xaml is very clean. A typical column definition for me looks like this (Where LineItemNumber is a public property on my LineItemViewModel):

<DataGridTextColumn Binding="{Binding Path=LineItemNumber, StringFormat=000}"
                              ElementStyle="{StaticResource CellRightAlign}"
                              Header="Line No." />

Upvotes: 0

Levelbit
Levelbit

Reputation: 170

        <my:DataGrid x:Name="locationGrid">
            <my:DataGrid.Columns>
                <my:DataGridTemplateColumn>
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=LocationName}"/>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                    <my:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                            <TextBox Text="{Binding Path=LocationName}"/>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellEditingTemplate>
                </my:DataGridTemplateColumn>
            </my:DataGrid.Columns>
        </my:DataGrid>    

You can put in each DataGrid column almost whatever you want. I gave you example here. You can define even header template. If you make small program there is no need for MVVM, but I don't understand you quite well why you need MV for DataGridRow? Make UserControl and embed it in DataTemplate, and for UserControl make VM class.

Upvotes: 1

Johnny
Johnny

Reputation: 1575

You can use DataTemplate for each row and customize it the way u need.

Upvotes: 0

Related Questions