开发者

How do I move repeating styles to resources section?

I have the following xaml-code.

<tk:DataGrid
    ItemsSource="{Binding Path=Products}"
    AutoGenerateColumns="False">
    <tk:DataGrid.Columns>
        <tk:DataGridTextColumn
                    Header="Id">
            <tk:DataGridTextColumn.CellStyle>
                <Style TargetType="{x:Type tk:DataGridCell}">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <TextBlock Margin="0,10"  Text="{Binding Path=Id}" />
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </tk:DataGridTextColumn.CellStyle>
        </tk:DataGridTextColumn>
        <tk:DataGridTextColumn
                    Header="Product">
            <tk:DataGridTextColumn.CellStyle>
                <Style TargetType="{x:Type tk:DataGridCell}">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <TextBlock Margin="0,10"  Text="{Binding Pat开发者_如何学Pythonh=Name}" />
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </tk:DataGridTextColumn.CellStyle>
        </tk:DataGridTextColumn>
    </tk:DataGrid.Columns>
</tk:DataGrid>

How can I get rid of copy-paste? The straightforward way of moving it to resources fails because Text="{Binding Path=XYZ}" stops working.


I don't think you can do what you want here, i.e. have one template definition for both cells. However, using a DataGridTemplateColumn will take out some lines of XAML (it's a bit strange using a DataGridTextColumn and then overwriting its control template anyway):

    <DataGrid ItemsSource="{Binding Path=Products}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="ID">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Margin="10,0" Text="{Binding Path=ID}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Name">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Margin="10,0" Text="{Binding Path=Name}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

There's another way of doing, this time using DataGridTextColum, which should give you something close to what you want too:

    <DataGrid Name="ui_datagrid" AutoGenerateColumns="False">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridCell}">
                <Setter Property="Padding" Value="0,10"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
        </DataGrid.Columns>
    </DataGrid>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜