如何为UniformGrid单元格添加边框

时间:2013-09-08 11:07:59

标签: c# wpf

我想在WPF UniformGrid中添加元素边框。我尝试了什么:

<Window.Resources>
    <DataTemplate x:Key="GridCell">
        <Border BorderBrush="DarkGray" BorderThickness="5"></Border>
    </DataTemplate>
</Window.Resources>

...和...

<UniformGrid Name="grid">
    <ItemsControl ItemTemplate="{StaticResource GridCell}"></ItemsControl>
</UniformGrid>

它不起作用(没有边框出现)。我想让UniformGrid的每个孩子(以编程方式创建的按钮,因此它们不会出现在这里)有边框。它需要看起来像一个网格......水平和垂直网格线描绘行和列。

1 个答案:

答案 0 :(得分:3)

<Grid>
        <ItemsControl ItemsSource="{Binding NumericalPatches}" >
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Rows="7" Columns="7"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="DimGray" BorderThickness="3">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <TextBlock Grid.Row="0" Grid.Column="0" Margin="5" Text="{Binding StringFormat=F4,Path=Red}" FontSize="14" Foreground="Red"/>
                            <TextBlock Grid.Row="0" Grid.Column="1" Margin="5" Text="{Binding StringFormat=F4,Path=Green}" FontSize="14" Foreground="Green"/>
                            <TextBlock Grid.Row="1" Grid.Column="0" Margin="5" Text="{Binding StringFormat=F4,Path=Blue}" FontSize="14" Foreground="Blue"/>
                            <TextBlock Grid.Row="1" Grid.Column="1" Margin="5" Text="{Binding StringFormat=F4,Path=Chroma}" FontSize="14" Foreground="White"/>

                        </Grid>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>