wpf网格线 - 改变风格 [英] wpf gridlines - changing style

查看:147
本文介绍了wpf网格线 - 改变风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法改变wpf网格中的网格线的样式?
我需要将网格划分为4个单元格。要做到这一点,我使用RowDefinitions和ColumnDefinitions。然而,我需要用户区分哪个单元格,这就是为什么我需要改变网格线的颜色。

在你看的目光。在WPF中,几乎任何事情都有不同的方法。这里有几个更容易的。



最简单的方法是设置ShowGridlines =True:

 < Grid Horizo​​ntalAlignment =Stretch
VerticalAlignment =Stretch
Margin =5
ShowGridLines =True>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>

< Grid.RowDefinitions>
< RowDefinition Height =*/>
< RowDefinition Height =*/>
< /Grid.RowDefinitions>

< TextBlock Grid.Column =0
Grid.Row =0
Text =(0,0)/>
< TextBlock Grid.Column =1
Grid.Row =0
Text =(1,0)/>
< TextBlock Grid.Column =0
Grid.Row =1
Text =(0,1)/>
< TextBlock Grid.Column =1
Grid.Row =1
Text =(1,0)/>
< / Grid>

这给了你一些网格:



您也可以在网格的每个单元格中使用Rectangle得到不同的效果。在这里,填充是透明的,笔画是蓝色的:

 < Grid Horizo​​ntalAlignment =Stretch
VerticalAlignment = 拉伸
保证金=5>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>

< Grid.RowDefinitions>
< RowDefinition Height =*/>
< RowDefinition Height =*/>
< /Grid.RowDefinitions>

< Rectangle Grid.Column =0
Grid.Row =0
Stroke =Blue
Fill =Transparent/>
< TextBlock Grid.Column =0
Grid.Row =0
Text =(0,0)/>
< Rectangle Grid.Column =1
Grid.Row =0
Stroke =Blue
Fill =Transparent/>
< TextBlock Grid.Column =1
Grid.Row =0
Text =(1,0)/>
< Rectangle Grid.Column =0
Grid.Row =1
Stroke =Blue
Fill =Transparent/>
< TextBlock Grid.Column =0
Grid.Row =1
Text =(0,1)/>
< Rectangle Grid.Column =1
Grid.Row =1
Stroke =Blue
Fill =Transparent/>
< TextBlock Grid.Column =1
Grid.Row =1
Text =(1,0)/>
< / Grid>

产生这种结果:

p>

 < Grid Horizo​​ntalAlignment =Stretch
VerticalAlignment =Stretch
Margin =5>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>

< Grid.RowDefinitions>
< RowDefinition Height =*/>
< RowDefinition Height =*/>
< /Grid.RowDefinitions>

< Rectangle Grid.Column =0
Grid.Row =0
Fill =LightBlue/>
< TextBlock Grid.Column =0
Grid.Row =0
Text =(0,0)/>
< Rectangle Grid.Column =1
Grid.Row =0
Fill =LightYellow/>
< TextBlock Grid.Column =1
Grid.Row =0
Text =(1,0)/>
< Rectangle Grid.Column =0
Grid.Row =1
Fill =LightYellow/>
< TextBlock Grid.Column =0
Grid.Row =1
Text =(0,1)/>
< Rectangle Grid.Column =1
Grid.Row =1
Fill =LightBlue/>
< TextBlock Grid.Column =1
Grid.Row =1
Text =(1,0)/>
< / Grid>

例如,可以给棋盘图案:



这绝不是一个全面的答案 - 你可以填写一本书。这只是为了表明有很多方法可以解决您的问题,并且如果您只需要这些解决方案,就可以使用一些相当快捷的解决方案。


Is there any way to change the style of gridlines in wpf grid? I need to divide grid into 4 cells. To do it I used RowDefinitions and ColumnDefinitions. However I need user to distinguish which cell is which, that's why I need to change the color of the gridlines.

解决方案

It depends on the look you are going for. In WPF, there are different ways to do almost anything. Here are a couple of the easier ones.

The easiest way is to set ShowGridlines="True":

    <Grid HorizontalAlignment="Stretch" 
          VerticalAlignment="Stretch" 
          Margin="5"
          ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <TextBlock Grid.Column="0" 
                   Grid.Row="0"
                   Text="(0,0)" />
        <TextBlock Grid.Column="1" 
                   Grid.Row="0"
                   Text="(1,0)" />
        <TextBlock Grid.Column="0" 
                   Grid.Row="1"
                   Text="(0,1)" />
        <TextBlock Grid.Column="1" 
                   Grid.Row="1"
                   Text="(1,0)" />
    </Grid>

That gives you grid something like:

You can also use a Rectangle in each cell of the grid to get different effects. Here, the Fill is transparent and the Stroke is Blue:

<Grid HorizontalAlignment="Stretch" 
      VerticalAlignment="Stretch" 
      Margin="5">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Rectangle Grid.Column="0"
               Grid.Row="0"  
               Stroke="Blue"
               Fill="Transparent" />
    <TextBlock Grid.Column="0" 
               Grid.Row="0"
               Text="(0,0)" />
    <Rectangle Grid.Column="1"
               Grid.Row="0"  
               Stroke="Blue"
               Fill="Transparent" />
    <TextBlock Grid.Column="1" 
               Grid.Row="0"
               Text="(1,0)" />
    <Rectangle Grid.Column="0"
               Grid.Row="1"  
               Stroke="Blue"
               Fill="Transparent" />
    <TextBlock Grid.Column="0" 
               Grid.Row="1"
               Text="(0,1)" />
    <Rectangle Grid.Column="1"
               Grid.Row="1"  
               Stroke="Blue"
               Fill="Transparent" />
    <TextBlock Grid.Column="1" 
               Grid.Row="1"
               Text="(1,0)" />
</Grid>

That produces this:

Alternatively, you can fill the Rectangles and not give them a Stroke:

    <Grid HorizontalAlignment="Stretch" 
          VerticalAlignment="Stretch" 
          Margin="5">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Rectangle Grid.Column="0"
                   Grid.Row="0"  
                   Fill="LightBlue" />
        <TextBlock Grid.Column="0" 
                   Grid.Row="0"
                   Text="(0,0)" />
        <Rectangle Grid.Column="1"
                   Grid.Row="0"  
                   Fill="LightYellow" />
        <TextBlock Grid.Column="1" 
                   Grid.Row="0"
                   Text="(1,0)" />
        <Rectangle Grid.Column="0"
                   Grid.Row="1"  
                   Fill="LightYellow" />
        <TextBlock Grid.Column="0" 
                   Grid.Row="1"
                   Text="(0,1)" />
        <Rectangle Grid.Column="1"
                   Grid.Row="1"  
                   Fill="LightBlue" />
        <TextBlock Grid.Column="1" 
                   Grid.Row="1"
                   Text="(1,0)" />
    </Grid>

That can, for instance, give a checkerboard pattern:

This is by no means a comprehensive answer - you could probably fill a book. It was just meant to show that there are many ways to do what you are asking, and that there are some pretty quick and easy solutions if that's all you need.

这篇关于wpf网格线 - 改变风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆