wpf - Customize the grid lines for original wpf Grid control

46 篇文章 0 订阅

The System.WIndows.Controls.Grid classes does provide a ShowGridLines method, while it does not provide means to override the style of the grid lines. 

 

 

Ian Oakes has shows some good ways to use the grid lines and his post is available at "Displaying Grid Lines

 

While it does show the quitessence of programming with styles, so here is what is the most important of the styles.

 

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <Style x:Key="lineStyle" TargetType="Line">

        <Setter Property="Stroke" Value="Gray" />
        <Setter Property="Stretch" Value="Fill" />
        <Setter Property="Grid.ZIndex" Value="100" />
        <Setter Property="StrokeDashArray" Value="1,2" /> <!-- DoubleValueCollection-->
    </Style>
    
    
    <Style x:Key="horizontalLineStyle" TargetType="Line" BasedOn="{StaticResource lineStyle}" >
        <Setter Property="X2" Value="1" />
        <Setter Property="VerticalAlignment" Value="Bottom" />
        <Setter Property="Grid.ColumnSpan" Value="{Binding Path=ColumnDefinitions.Count, RelativeSource={RelativeSource AncestorType=Grid}}" />
    </Style>

    <Style x:Key="verticalLineStyle" TargetType="Line" BasedOn="{StaticResource lineStyle}" >
        <Setter Property="Y2" Value="1" />
        <Setter Property="HorizontalAlignment" Value="Right" />
        <Setter Property="Grid.RowSpan" Value="{Binding Path=RowDefinitions.Count, RelativeSource={RelativeSource AncestorType=Grid}}" />
    </Style>

    <Style x:Key="verticalLineStyleLeft" TargetType="Line" BasedOn="{StaticResource verticalLineStyle}" >
        <Setter Property="HorizontalAlignment" Value="Left" />
    </Style>

    <Style x:Key="horizontalLineStyleTop" TargetType="Line" BasedOn="{StaticResource horizontalLineStyle}" >
        <Setter Property="VerticalAlignment" Value="Top" />
    </Style>
    

</ResourceDictionary>

 

 

and how it is used? how will the styles be applied to draw the grid lines?

 

<Window x:Class="GridLines.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid
        x:Name="Grid" Margin="20"
        >
        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="20" />
        </Grid.ColumnDefinitions>
        
        
        <Line Grid.Column="0" Style="{StaticResource verticalLineStyleLeft}" />
        <Line Grid.Column="0" Style="{StaticResource verticalLineStyle}" />
        <Line Grid.Column="1" Style="{StaticResource verticalLineStyle}" />
        <Line Grid.Column="2" Style="{StaticResource verticalLineStyle}" />
        <Line Grid.Column="3" Style="{StaticResource verticalLineStyle}" />
        
        <Line Grid.Row="0" Style="{StaticResource horizontalLineStyleTop}" />
        <Line Grid.Row="0" Style="{StaticResource horizontalLineStyle}" />
        <Line Grid.Row="1" Style="{StaticResource horizontalLineStyle}" />
        <Line Grid.Row="2" Style="{StaticResource horizontalLineStyle}" />
        <Line Grid.Row="3" Style="{StaticResource horizontalLineStyle}" />

    </Grid>
</Window>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值