如何在ListView下的Gridview中设置单元格样式

时间:2013-08-22 04:27:57

标签: c# wpf xml

如何为Gridview中的每个单元格设置样式。我希望我的单元格文本位于带有红色背景的圆角文本框中。我的代码是

<ListView Margin="0"  ItemsSource="{Binding ParamList}" >
    <ListView.View >
        <GridView  >
            <GridView.ColumnHeaderTemplate>
                <DataTemplate>
                    <Border  BorderThickness="2" CornerRadius="5" Height="75" MinWidth="60"  >
                        <Border.Background>Gray</Border.Background>
                        <TextBlock Foreground="WhiteSmoke" Margin="5" 
                                   Text="{Binding}" Width="Auto" 
                                   HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                </DataTemplate>
            </GridView.ColumnHeaderTemplate>

            <GridViewColumn Header="category1" DisplayMemberBinding="{Binding Path=param1}"   />
            <GridViewColumn Header="category2" DisplayMemberBinding="{Binding Path=param1}" />

        </GridView>
    </ListView.View>
</ListView>

1 个答案:

答案 0 :(得分:0)

使用GridViewColumn的CellTemplate属性设置包含TextBox的DataTemplate,并为每个列设置适当的样式。我不相信有一种方法可以在Gridview级别设置它,因此您不必单独在每个列上设置它。

 <Grid>
    <ListView ItemsSource="{Binding Items}">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Background="Red" Text="{Binding Item1}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Item2}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>