Using DataGridTemplateColumns as StaticResources

To use a DataGridTemplateColumns as StaticResources is not as simple as it sounds, because the property has no setter. But there is a quick work around to avoid much pasting:

<Window.Resources>
    <DataGridTemplateColumn x:Key="clmOne" x:Shared="False" Header="H1">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox Text="{Binding C1}"></TextBox>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn x:Key="clmTwo" x:Shared="False" Header="H2">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox Text="{Binding C2}"></TextBox>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</Window.Resources>

<Grid Height="337">
    <DataGrid AutoGenerateColumns="False" Name="dataGrid1" ItemsSource="{Binding MyTestDataTable,  NotifyOnSourceUpdated=True, Mode=TwoWay}">
        <DataGrid.Columns>
            <StaticResource ResourceKey="clmOne"/>
            <StaticResource ResourceKey="clmTwo"/>
        </DataGrid.Columns>
    </DataGrid>

    <Button Content="Reload" Name="button1" Command="{Binding ReloadCommand}" />
    <DataGrid AutoGenerateColumns="False" Name="dataGrid2" ItemsSource="{Binding MyTestDataTable,  NotifyOnSourceUpdated=True, Mode=TwoWay}">
        <DataGrid.Columns>
            <StaticResource ResourceKey="clmOne"/>
            <StaticResource ResourceKey="clmTwo"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
Advertisements
This entry was posted in Development, WPF and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s