zoukankan      html  css  js  c++  java
  • WPF笔记: 自定义DataGrid样式

    直接xmal style:

    <Style x:Key="GridStyle" TargetType="DataGrid">
            <Setter Property="BorderBrush" Value="Gray"></Setter>
            <Setter Property="BorderThickness" Value="0,1"></Setter>
            <Setter Property="AutoGenerateColumns" Value="False"></Setter>
            <Setter Property="ScrollViewer.CanContentScroll" Value="True"></Setter>
            <Setter Property="GridLinesVisibility" Value="Horizontal"></Setter>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"></Setter>
            <Setter Property="SelectionMode" Value="Single"></Setter>
            <Setter Property="SelectionUnit" Value="FullRow"></Setter>
            
            <Setter Property="CanUserDeleteRows" Value="False"></Setter>
            <Setter Property="CanUserSortColumns" Value="False"></Setter>
            <Setter Property="CanUserReorderColumns" Value="False"></Setter>
            <Setter Property="IsReadOnly" Value="True"></Setter>
            <Setter Property="CanUserResizeColumns" Value="False"></Setter>
            <Setter Property="CanUserResizeRows" Value="False"></Setter>
            <Setter Property="CanUserAddRows" Value="False"></Setter>
            <Setter Property="Background" Value="DarkGray"></Setter>
            <Setter Property="Foreground" Value="White"></Setter>
    
            <Setter Property="CellStyle">
                <Setter.Value>
                    <Style TargetType="DataGridCell">
                        <Setter Property="BorderThickness" Value="0"></Setter>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="DataGridCell">
                                    <Border  BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" Value="Silver"></Setter>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Background" Value="SlateGray"></Setter>
                            </Trigger>
    
                        </Style.Triggers>
                    </Style>
                </Setter.Value>
            </Setter>
            <Setter Property="ColumnHeaderStyle">
                <Setter.Value>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="Background" Value="Transparent"></Setter>
                        <Setter Property="HorizontalAlignment" Value="Center"></Setter>
    
                    </Style>
                </Setter.Value>
            </Setter>
    
            <Setter Property="RowStyle">
                <Setter.Value>
                    <Style TargetType="DataGridRow">
                        <Setter Property="Height" Value="45"></Setter>
                        <Setter Property="Background" Value="Gray"></Setter>
                        <Setter Property="Foreground" Value="White"></Setter>
    
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" Value="Silver"></Setter>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Background" Value="SlateGray"></Setter>
                            </Trigger>
    
                        </Style.Triggers>
                    </Style>
                </Setter.Value>
            </Setter>
    
        </Style>

    使用:

     <DataGrid Style="{StaticResource GridStyle}" ItemsSource="{Binding Path=Pacses}" >
                    <DataGrid.Columns>
                        
                        <DataGridTextColumn  Header="Name" Width="3*" MinWidth="120" Binding="{Binding Name}" />
                        <DataGridTextColumn  Header="IP" Width="3*" MinWidth="120" Binding="{Binding IP}" />
                        <DataGridTextColumn Header="PORT" Width="3*" MinWidth="120" Binding="{Binding Port}"/>
    
                        
                        
                        <DataGridTemplateColumn Header="#" Width="4*" MinWidth="160">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                        <ToggleSwitchButton IsChecked="{Binding IsDefault}" ToolTip="Set Default"                                                                 
                                                                CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=SelectedItem}"
                                                               Command="{Binding ElementName=PacsSettingPage,Path=DataContext.SetDefaultPacsCmd}">
                                            
                                        </ToggleSwitchButton>
                                        
                                    </StackPanel>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>
  • 相关阅读:
    普通平衡树(AVL树)
    正态分布与二项分布
    grahql操作
    skywalking定时删除在大并发下引起的性能问题
    Apache SkyWalking SQL注入漏洞复现分析 (CVE-2020-9483)
    skywalking远程调试Apache SkyWalking SQL注入
    sourceTree详细使用教程
    git 分支的相关操作、创建分支、查看分支、切换分支、删除分支
    [Git高级教程 (一)] 通过Tag标签回退版本修复bug
    idea 版本2018.3.6版本的安装
  • 原文地址:https://www.cnblogs.com/coder-fang/p/10239389.html
Copyright © 2011-2022 走看看