I have an application with two DataGrid
controls. And although their ItemsSource
are bound to different collections, their item type, columns, styles and event handlers are exactly the same.
As their XAML codes are growing bigger and bigger, how can I create the same "content" and reuse it in both DataGrids?
What I currently have is:
<DataGrid x:Name="LeftGrid"
ItemsSource="{Binding LeftCollection}"
Grid.Column="0"
CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False">
<!--exactly the same code from this forward-->
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="DataGridRow_MouseDoubleClick"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Type" Binding="{Binding Type, Mode=OneWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Status" Binding="{Binding Status, Mode=OneWay}" IsReadOnly="True"/>
...............................
................................
.....................................
</DataGrid.Columns>
</DataGrid>
Then I have "exaclty" the same datagrid copied whose only changes are Name
, Grid.Column
and ItemsSource
(only the three first rows of the XAML):
<DataGrid x:Name="RightGrid"
ItemsSource="{Binding RightCollection}"
Grid.Column="1"
CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False">
<!--exactly the same code from this forward-->
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="DataGridRow_MouseDoubleClick"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Type" Binding="{Binding Type, Mode=OneWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Status" Binding="{Binding Status, Mode=OneWay}" IsReadOnly="True"/>
...............................
................................
.....................................
</DataGrid.Columns>
</DataGrid>