虚线水平网格线在WPF的DataGrid虚线、网格、水平、DataGrid

2023-09-06 19:13:30 作者:∑gray°鱼ル

有没有一种方法,以对在WPF数据网格的水平网格线虚线?我用Google搜索无处不在,似乎无法找到这样做的具体办法。一个解决方案可以解决该是设置Datagrid.RowStyle。我曾尝试这样做,它会导致错误。我已经绑定我的DataGrid到数据表。 2列只是文字和第三的是image列。对于此专栏中,我用DataGridTemplateColumn。文本块和图像。我希望你能帮助我的人......如果你想要我的code样品在这儿呢。

 < StackPanel的>
                < D​​G:DataGrid的名称=questionList
                             HeadersVisibility =无
                             的AutoGenerateColumns =FALSE
                             背景=白
                             保证金=42,32,43,0
                             了borderThickness =0
                             GridLinesVisibility =水平
                             CanUserAddRows =假
                             Horizo​​ntalGridLinesBrush =#FFCCCCCC
                             =了maxHeight549
                             光标=手
                             previewMouseLeftButtonUp =questionnaireList_ previewMouseLeftButtonUp>
                    < D​​G:DataGrid.CellStyle>
                        <风格的TargetType ={X:DG型:DataGridCell}>
                            < setter属性=了borderThickness值=0/>
                        < /样式和GT;
                    < / DG:DataGrid.CellStyle>
                    < D​​G:DataGrid.RowStyle>
                        <风格的TargetType ={X:DG型:DataGridRow}>
                            < setter属性=背景VALUE ={结合MYIMAGE,转换器= {X:静态我:StatusColorConverter.instance}}/>
                        < /样式和GT;
                    < / DG:DataGrid.RowStyle>
                    < D​​G:DataGrid.Columns>
                        < D​​G:DataGridTemplateColumn WIDTH =69 *>
                            < D​​G:DataGridTemplateColumn.CellTemplate>
                                <的DataTemplate>
                                    < TextBlock的保证金=12,16,0,17
                                               字号=18
                                               前景=#0891F1
                                               文本={绑定路径=号}
                                               TextWrapping =自动换行/>
                                < / DataTemplate中>
                            < / DG:DataGridTemplateColumn.CellTemplate>
                        < / DG:DataGridTemplateColumn>
                        &所述; DG:DataGridTemplateColumn宽度=601 *>
                            < D​​G:DataGridTemplateColumn.CellTemplate>
                                <的DataTemplate>
                                    < TextBlock的字号=16前景=#666666
                                               文本={绑定路径=说明}
                                               TextWrapping =自动换行
                                               TextAlignment =对齐
                                               保证金=0,16,0,17/>
                                < / DataTemplate中>
                            < / DG:DataGridTemplateColumn.CellTemplate>
                        < / DG:DataGridTemplateColumn>
                        < D​​G:DataGridTemplateColumn WIDTH =117 *>
                            < D​​G:DataGridTemplateColumn.CellTemplate>
                                <的DataTemplate>
                                    <图像源={绑定路径= IMGS}
                                           拉伸=无
                                           VerticalAlignment =热门
                                           的Horizo​​ntalAlignment =右
                                           保证金=0,16,18,17/>
                                < / DataTemplate中>
                            < / DG:DataGridTemplateColumn.CellTemplate>
                        < / DG:DataGridTemplateColumn>
                    < / DG:DataGrid.Columns>
                < / DG:数据网格>
            < / StackPanel的>
 

解决方案

您可以禁用这些通过指定 GridLinesVisibility =垂直在的DataGrid 。然后,您可以重新模板 DataGridRow ,并在每行的末尾添加虚线

是这样的:

 <数据网格GridLinesVisibility =垂直>
    < D​​ataGrid.RowStyle>
        <风格的TargetType =DataGridRow>
            < setter属性=模板>
                < Setter.Value>
                    <的ControlTemplate的TargetType ={X:类型DataGridRow}>
                        < BORDER X:名称=DGR_BorderBorderBrush ={TemplateBinding BorderBrush}了borderThickness ={TemplateBinding了borderThickness}背景={TemplateBinding背景}SnapsToDevicePixels =真>
                            < SelectiveScrollingGrid>
                                < SelectiveScrollingGrid.ColumnDefinitions>
                                    < ColumnDefinition宽度=自动/>
                                    &所述; ColumnDefinition宽度=*/>
                                < /SelectiveScrollingGrid.ColumnDefinitions>
                                < SelectiveScrollingGrid.RowDefinitions>
                                    < RowDefinition身高=*/>
                                    < RowDefinition高度=自动/>
                                    < RowDefinition高度=自动/>
                                < /SelectiveScrollingGrid.RowDefinitions>
                                &所述; DataGridCells presenter Grid.Column =1ItemsPanel ={TemplateBinding ItemsPanel}SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}/>
                                &所述; DataGridDetails presenter Grid.Column =1Grid.Row =1SelectiveScrollingGrid.SelectiveScrollingOrientation ={结合AreRowDetailsFrozen,ConverterParameter = {X:静态SelectiveScrollingOrientation.Vertical},转换器= {X:静态DataGrid.RowDetailsS​​crollingConverter }的RelativeSource = {的RelativeSource AncestorType = {X:类型的DataGrid}}}能见度={TemplateBinding DetailsVisibility}​​/>
                                < D​​ataGridRowHeader Grid.RowSpan =2SelectiveScrollingGrid.SelectiveScrollingOrientation =垂直能见度={结合HeadersVisibility,ConverterParameter = {X:静态DataGridHeadersVisibility.Row},转换器= {X:静态DataGrid.HeadersVisibilityConverter}的RelativeSource = {的RelativeSource AncestorType = {X:类型的DataGrid}}}/>
                                &其中;路径Grid.Row =2Grid.ColumnSpan =2
                                      数据=M0,0.5 L1,0.5
                                      拉伸=填充行程=黑StrokeThickness =1
                                      StrokeDashArray =1.0 2.0/>
                            < / SelectiveScrollingGrid>
                        < /边框>
                    < /控件模板>
                < /Setter.Value>
            < /二传手>
        < /样式和GT;
    < /DataGrid.RowStyle>
    <  - !...  - >
< /数据网格>
 
如图.网格中的每一个方格的边长都相等.点A.B.C都在网格的格点上.按要求完成下列各小题. 1 画线段AB.延长线段BA到点D.使得DA AB,的基础上.连接CB.CD.组成三角形

编辑:以下是模板为3.5 的DataGrid 在工具箱

 <自定义:数据网格GridLinesVisibility =垂直>
    <自定义:DataGrid.RowStyle>
        <风格的TargetType =自定义:DataGridRow>
            < setter属性=模板>
                < Setter.Value>
                    <的ControlTemplate的TargetType ={X:定制类型:DataGridRow}>
                        < BORDER X:名称=DGR_BorderBorderBrush ={TemplateBinding BorderBrush}了borderThickness ={TemplateBinding了borderThickness}背景={TemplateBinding背景}SnapsToDevicePixels =真>
                            <自定义:SelectiveScrollingGrid>
                                <自定义:SelectiveScrollingGrid.ColumnDefinitions>
                                    < ColumnDefinition宽度=自动/>
                                    &所述; ColumnDefinition宽度=*/>
                                < /自定义:SelectiveScrollingGrid.ColumnDefinitions>
                                <自定义:SelectiveScrollingGrid.RowDefinitions>
                                    < RowDefinition身高=*/>
                                    < RowDefinition高度=自动/>
                                    < RowDefinition高度=自动/>
                                < /自定义:SelectiveScrollingGrid.RowDefinitions>
                                &所述;自定义:DataGridCells presenter Grid.Column =1ItemsPanel ={TemplateBinding ItemsPanel}SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}/>
                                &所述;自定义:DataGridDetails presenter Grid.Column =1Grid.Row =1能见度={TemplateBinding DetailsVisibility}>
                                    <自定义:SelectiveScrollingGrid.SelectiveScrollingOrientation>
                                        <绑定路径=AreRowDetailsFrozen的RelativeSource ={的RelativeSource FindAncestor,AncestorLevel = 1,AncestorType = {X:定制类型:数据网格}}>
                                            < Binding.ConverterParameter>
                                                <自定义:SelectiveScrollingOrientation>垂直< /自定义:SelectiveScrollingOrientation>
                                            < /Binding.ConverterParameter>
                                        < /装订>
                                    < /自定义:SelectiveScrollingGrid.SelectiveScrollingOrientation>
                                < /自定义:DataGridDetails presenter>
                                <自定义:DataGridRowHeader Grid.RowSpan =2自定义:SelectiveScrollingGrid.SelectiveScrollingOrientation =垂直>
                                    <自定义:DataGridRowHeader.Visibility>
                                        <绑定路径=HeadersVisibility的RelativeSource ={的RelativeSource FindAncestor,AncestorLevel = 1,AncestorType = {X:定制类型:数据网格}}>
                                            < Binding.ConverterParameter>
                                                <自定义:DataGridHeadersVisibility>行< /自定义:DataGridHeadersVisibility>
                                            < /Binding.ConverterParameter>
                                        < /装订>
                                    < /自定义:DataGridRowHeader.Visibility>
                                < /自定义:DataGridRowHeader>
                                &其中;路径Grid.Row =2Grid.ColumnSpan =2
                                      数据=M0,0.5 L1,0.5
                                      拉伸=填充行程=黑StrokeThickness =1
                                      StrokeDashArray =1.0 2.0/>
                            < /自定义:SelectiveScrollingGrid>
                        < /边框>
                    < /控件模板>
                < /Setter.Value>
            < /二传手>
        < /样式和GT;
    < /自定义:DataGrid.RowStyle>
    <  - !...  - >
< /自定义:数据网格>
 

Is there a way to have a dotted line on the horizontal grid lines in WPF datagrid? I have googled everywhere and cannot seem to find a concrete way of doing so. One solution that may solve this is setting the Datagrid.RowStyle. I have tried this and it causes error. I have binded my datagrid to a datatable. 2 columns are just text and the 3rd one is an image column. For this columns, I used DataGridTemplateColumn. Textblock and Image. I hope you can help me guys...If you want a sample of my code here it is.

 <StackPanel>
                <dg:DataGrid Name="questionList"
                             HeadersVisibility="None"
                             AutoGenerateColumns="False"
                             Background="White"
                             Margin="42,32,43,0"
                             BorderThickness="0" 
                             GridLinesVisibility="Horizontal"
                             CanUserAddRows="False" 
                             HorizontalGridLinesBrush="#FFCCCCCC" 
                             MaxHeight="549" 
                             Cursor="Hand" 
                             PreviewMouseLeftButtonUp="questionnaireList_PreviewMouseLeftButtonUp">
                    <dg:DataGrid.CellStyle>
                        <Style TargetType="{x:Type dg:DataGridCell}">
                            <Setter Property="BorderThickness" Value="0"/>
                        </Style>
                    </dg:DataGrid.CellStyle>
                    <dg:DataGrid.RowStyle>
                        <Style TargetType="{x:Type dg:DataGridRow}">
                            <Setter Property="Background" Value="{Binding MyImage, Converter={x:Static my:StatusColorConverter.instance}}" />
                        </Style>
                    </dg:DataGrid.RowStyle>
                    <dg:DataGrid.Columns>
                        <dg:DataGridTemplateColumn Width="69*">
                            <dg:DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Margin="12,16,0,17" 
                                               FontSize="18" 
                                               Foreground="#0891F1" 
                                               Text="{Binding Path=Number}" 
                                               TextWrapping="Wrap"/>
                                </DataTemplate>
                            </dg:DataGridTemplateColumn.CellTemplate>
                        </dg:DataGridTemplateColumn>
                        <dg:DataGridTemplateColumn Width="601*">
                            <dg:DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock FontSize="16" Foreground="#666666"
                                               Text="{Binding Path=Desc}"
                                               TextWrapping="Wrap"
                                               TextAlignment="Justify"
                                               Margin="0,16,0,17" />
                                </DataTemplate>
                            </dg:DataGridTemplateColumn.CellTemplate>
                        </dg:DataGridTemplateColumn>
                        <dg:DataGridTemplateColumn Width="117*">
                            <dg:DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Image Source="{Binding Path=Imgs}" 
                                           Stretch="None" 
                                           VerticalAlignment="Top" 
                                           HorizontalAlignment="Right" 
                                           Margin="0,16,18,17" />
                                </DataTemplate>
                            </dg:DataGridTemplateColumn.CellTemplate>
                        </dg:DataGridTemplateColumn>
                    </dg:DataGrid.Columns>
                </dg:DataGrid>
            </StackPanel>

解决方案

You can disable the Horizontal Grid Lines which are drawn in code by specifying GridLinesVisibility="Vertical" in the DataGrid. You could then re-template DataGridRow and add the dashed line at the end of each row

Looks like this:

<DataGrid GridLinesVisibility="Vertical">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridRow}">
                        <Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <SelectiveScrollingGrid>
                                <SelectiveScrollingGrid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </SelectiveScrollingGrid.ColumnDefinitions>
                                <SelectiveScrollingGrid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </SelectiveScrollingGrid.RowDefinitions>
                                <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
                                <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                <Path Grid.Row="2" Grid.ColumnSpan="2"
                                      Data="M0,0.5 L1,0.5"
                                      Stretch="Fill" Stroke="Black" StrokeThickness="1"
                                      StrokeDashArray="1.0 2.0"/>
                            </SelectiveScrollingGrid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.RowStyle>
    <!-- ... -->
</DataGrid>

Edit: Here is the Template for the 3.5 DataGrid in the toolkit

<Custom:DataGrid GridLinesVisibility="Vertical">
    <Custom:DataGrid.RowStyle>
        <Style TargetType="Custom:DataGridRow">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Custom:DataGridRow}">
                        <Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <Custom:SelectiveScrollingGrid>
                                <Custom:SelectiveScrollingGrid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Custom:SelectiveScrollingGrid.ColumnDefinitions>
                                <Custom:SelectiveScrollingGrid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Custom:SelectiveScrollingGrid.RowDefinitions>
                                <Custom:DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                <Custom:DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" Visibility="{TemplateBinding DetailsVisibility}">
                                    <Custom:SelectiveScrollingGrid.SelectiveScrollingOrientation>
                                        <Binding Path="AreRowDetailsFrozen" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Custom:DataGrid}}">
                                            <Binding.ConverterParameter>
                                                <Custom:SelectiveScrollingOrientation>Vertical</Custom:SelectiveScrollingOrientation>
                                            </Binding.ConverterParameter>
                                        </Binding>
                                    </Custom:SelectiveScrollingGrid.SelectiveScrollingOrientation>
                                </Custom:DataGridDetailsPresenter>
                                <Custom:DataGridRowHeader Grid.RowSpan="2" Custom:SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical">
                                    <Custom:DataGridRowHeader.Visibility>
                                        <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Custom:DataGrid}}">
                                            <Binding.ConverterParameter>
                                                <Custom:DataGridHeadersVisibility>Row</Custom:DataGridHeadersVisibility>
                                            </Binding.ConverterParameter>
                                        </Binding>
                                    </Custom:DataGridRowHeader.Visibility>
                                </Custom:DataGridRowHeader>
                                <Path Grid.Row="2" Grid.ColumnSpan="2"
                                      Data="M0,0.5 L1,0.5"
                                      Stretch="Fill" Stroke="Black" StrokeThickness="1"
                                      StrokeDashArray="1.0 2.0"/>
                            </Custom:SelectiveScrollingGrid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Custom:DataGrid.RowStyle>
    <!-- ... -->
</Custom:DataGrid>