I have some requirement to customize datagrid, so i create my own dataGrid extending WPF datagrid. Small relevant code posted below -
public class ExtendedDataGrid : DataGrid
{
public ExtendedDataGrid()
{
this.SelectionMode = DataGridSelectionMode.Extended;
}
}
I create its instance in a window and set SelectionMode to Single which works perfectly fine and property gets set to Single for the dataGrid. So far all good.
But in case i place my DataGrid in ControlTemplate, SelectionMode never gets set to Single. SelectionMode is just for example no DP is getting set via XAML if i explicitly set that value in constructor of DataGrid.
Small sample replicating the problem is here -
<Grid>
<Grid.Resources>
<ControlTemplate x:Key="MyTemplate">
<local:ExtendedDataGrid ItemsSource="{Binding Collection,
RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType=Window}}"
SelectionMode="Single">
<local:ExtendedDataGrid.Columns>
<DataGridTextColumn Binding="{Binding}"/>
</local:ExtendedDataGrid.Columns>
</local:ExtendedDataGrid>
</ControlTemplate>
</Grid.Resources>
<ContentControl Template="{StaticResource MyTemplate}"/>
<local:ExtendedDataGrid ItemsSource="{Binding Collection,
RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType=Window}}"
Grid.Row="1" SelectionMode="Single">
<local:ExtendedDataGrid.Columns>
<DataGridTextColumn Binding="{Binding}"/>
</local:ExtendedDataGrid.Columns>
</local:ExtendedDataGrid>
</Grid>
For second DataGrid its working fine but not working for DataGrid placed inside ControlTemplate. Why this weird behaviour? Is it some bug in DataGrid code?
Note - It will work fine if i comment the line from DataGrid constructor where i am explicitly setting SelectionMode to Extended. I know that's default value and after removing that it will work fine for both cases (also there are many ways to set default value) but i want to know why it works in one case and not in other.