invisal
invisal

Reputation: 11171

Apply WPF Style to certain data grid column

I have this DataGrid

<DataGrid Name="OrderItemList" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Code" Binding="{Binding Code}" IsReadOnly="True" />
        <DataGridTextColumn Header="Color" IsReadOnly="True" />
        <DataGridTextColumn Header="Size" IsReadOnly="True" />
        <DataGridTextColumn Header="Price" Binding="{Binding Price`}" IsReadOnly="True" />
        <DataGridTextColumn Header="Qty" Binding="{Binding Qty}" />

        <DataGridTemplateColumn Header="">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button>Remove</Button>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>    

        <DataGridTextColumn Header="" Width="*" IsReadOnly="True" />
    </DataGrid.Columns>
</DataGrid>

This is my style

<Style x:Key="TextColumn" TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <Trigger Property="DataGridCell.IsSelected" Value="True">
            <Setter Property="Background" Value="#F1F1F1" />
        </Trigger>
    </Style.Triggers>

    <Setter Property="Padding" Value="10" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text, Mode=TwoWay, UpdateSourceTrigger=Default}"
                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}"
                    BorderThickness="0" IsReadOnly="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Column.IsReadOnly}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

How can I apply this style to only DataGridTextColumn's cell?

Upvotes: 0

Views: 1453

Answers (1)

Mike Eason
Mike Eason

Reputation: 9713

You can simply set the CellStyle property on your DataGridTextColumn definition.

<DataGridTextColumn CellStyle="{StaticResource TextColumn}" ...

Upvotes: 2

Related Questions