相关文章推荐

WPF新手小白随笔-DataGrid属性及样式设置

最近项目刚刚接触WPF,没有系统学习直接看代码,边学边改。总结一些关于学习中的基础知识点,主要是针对UI修改的。包括自己去网上找的一些方法,实现想要的效果。

下面来介绍一下关于DataGrid一些属性设置及使用方法:

<DataGrid x:Name="lbFileList"  CanUserAddRows="False" IsReadOnly="True" RowHeaderWidth="0" <!--去除多余header显示-->
AlternationCount="2"<!--隔行变色--> 
AutoGenerateColumns="False"<!--不需要根据数据源来自动生成列--> 
HorizontalAlignment="Stretch" 
GridLinesVisibility="None" BorderThickness="0" <!--去除边框线--> >
//WPF中使DataGrid 列宽填充表格方法,设置ColumnWidth属性为ColumnWidth="*"即可。
    <DataGrid.Columns>
        <DataGridTextColumn Header="名称" Binding="{Binding Path=Name}" Width="*"/>
        <DataGridTextColumn Header="大小" Binding="{Binding Path=FileSize}" Width="*"/>
        <DataGridTextColumn Header="描述" Binding="{Binding Path=Description}" Width="*"/>
        <DataGridTextColumn Header="类型" Binding="{Binding Path=Type}" Width="*"/>
    </DataGrid.Columns>
    <DataGrid.Resources>
        <!--设置行样式-->
        <!--背景色必须先设置cellstyle因为cellstyle会覆盖rowstyle样式-->
        <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="White"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="Foreground" Value="Black"/>
            <Style.Triggers>
                <!--隔行显示-->
                <Trigger Property="AlternationIndex" Value="0">
                    <Setter Property="Background" Value="#e7e7e7"/>
                </Trigger>
                <Trigger Property="AlternationIndex" Value="1">
                    <Setter Property="Background" Value="White"/>
                </Trigger>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="AntiqueWhite"/>
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="AliceBlue"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Resources>
</DataGrid>

输出效果如下:

主要实现消除列分割线、列表单双行显示颜色区分,以及点击选中和鼠标移动的显示。


还有一个同上面差不多,添加了全选框,实现选中删除。不过有个缺点是需要按住Ctrl键实现多选。

<DataGrid Name="dtGridOnLine"  MaxHeight="800" RowHeaderWidth="0" AlternationCount="2"  AutoGenerateColumns="False" CanUserAddRows="False" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" >
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="#DBEDF8"/>
                            <Setter Property="BorderBrush" Value="#DBEDF8"/>
                            <Setter Property="Foreground" Value="Black"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.Resources>
                <!--设置行样式-->
                <!--背景色必须先设置cellstyle因为cellstyle会覆盖rowstyle样式-->
                <Style TargetType="DataGridRow">
                    <Setter Property="Background" Value="White"/>
                    <Setter Property="Height" Value="25"/>
                    <Setter Property="Foreground" Value="Black"/>
                    <Style.Triggers>
                        <!--隔行显示-->
                        <Trigger Property="AlternationIndex" Value="0">
                            <Setter Property="Background" Value="#e7e7e7"/>
                        </Trigger>
                        <Trigger Property="AlternationIndex" Value="1">
                            <Setter Property="Background" Value="White"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="AntiqueWhite"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="AliceBlue"></Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>
                 //添加单选框列
                <DataGridCheckBoxColumn Binding="{Binding IsSelected,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}}">
			                    //表头添加单选框
                    <DataGridCheckBoxColumn.Header>
                        <CheckBox  Content="全选" Checked="CheckedAll_Click" Unchecked="UnCheckedAll_Click"/>
                    </DataGridCheckBoxColumn.Header>
                </DataGridCheckBoxColumn>
                <DataGridTextColumn Header="在线状态"  Binding="{Binding OperationType}"  IsReadOnly="True" Width="1*"/>
                <DataGridTextColumn Header="用户名称"  Binding="{Binding UserName}"  IsReadOnly="True" Width="1*"  />
                <DataGridTextColumn Header="IP地址"  Binding="{Binding UserIp}"  IsReadOnly="True" Width="1*" />
                <DataGridTextColumn Header="计算机名"  Binding="{Binding MachineName}"  IsReadOnly="True" Width="1*" />
                <DataGridTextColumn Header="在线/离线时间"  Binding="{Binding LogDate}"  IsReadOnly="True" Width="1*"/>
                <DataGridTextColumn Header="系统名称"  Binding="{Binding SystemName}"  IsReadOnly="True" Width="1*"  />
            </DataGrid.Columns>
        </DataGrid>

C#代码

				/// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckedAll_Click(object sender, RoutedEventArgs e)
            this.dtGridOnLine.SelectAll();
        /// <summary>
        /// 全不选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UnCheckedAll_Click(object sender, RoutedEventArgs e)
            this.dtGridOnLine.UnselectAll();
        /// <summary>
        /// 删除,为页面中某个按钮添加click事件来执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemove_Click(object sender, System.Windows.RoutedEventArgs e)
            List<Sys_Log_Operation> sysLog = this.dtGridOnLine.SelectedItems as List<Sys_Log_Operation>;//获取选中的列表项,方法需要在类里面定义
            if (sysLog == null || sysLog.Count==0)
                ControlUtility.ShowInfomationMsgbox("请选择需要删除的登录信息");
                return;
            foreach (var item in sysLog)
                    if (item.Delete())//在数据库中删除
                        sourceList.Remove(item);//从显示列表中移除
 
推荐文章