Wpf之HandyControls与MaterialDesign混用之DataGrid
首先在App.Xaml引入相关资源
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--Handy Control-->
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/>
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>
<!--Material Design-->
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/materialdesigncolor.blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
<!--Geometry-->
<ResourceDictionary Source="pack://application:,,,/Resources/Icons/Icons.xaml"/>
<!--Custom Styles-->
<ResourceDictionary Source="pack://application:,,,/Resources/Theme/AppBaseStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<local:AppLocator x:Key="Locator"/>
</ResourceDictionary>
</Application.Resources>
定义好DataGrid Style在引入到AppBaseStyle中, 下面是DataGrid.xaml的定义
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:hc="https://handyorg.github.io/handycontrol">
<!--<Style x:Key="LSTextBlockComboBoxStyle" TargetType="ComboBox">
<Setter Property="Focusable" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<TextBlock IsHitTestVisible="False" Text="{TemplateBinding Text}" TextTrimming="CharacterEllipsis" VerticalAlignment="Center"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>-->
<Style x:Key="LSTextBlockComboBoxStyle"
TargetType="{x:Type ComboBox}"
BasedOn="{StaticResource MaterialDesignDataGridComboBox}">
<Setter Property="IsHitTestVisible" Value="False" />
<Setter Property="Focusable" Value="False" />
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style x:Key="LSTextBlockComboBoxEditingStyle"
TargetType="{x:Type ComboBox}"
BasedOn="{StaticResource MaterialDesignDataGridComboBox}">
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style x:Key="LSDataGridTextColumnStyle" TargetType="TextBlock">
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style TargetType="DataGrid">
<Setter Property="Background" Value="{DynamicResource MaterialDesignPaper}"/>
<Setter Property="Foreground" Value="{DynamicResource MaterialDesignBody}"/>
<Setter Property="BorderBrush" Value="{DynamicResource MaterialDesignTextBoxBorder}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="EnableRowVirtualization" Value="True"/>
<Setter Property="CanUserAddRows" Value="False"/>
<Setter Property="CanUserDeleteRows" Value="False"/>
<Setter Property="CanUserReorderColumns" Value="False"/>
<Setter Property="CanUserResizeRows" Value="False"/>
<Setter Property="FontSize" Value="13"/>
<Setter Property="GridLinesVisibility" Value="Horizontal" />
<Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/>
<Setter Property="HeadersVisibility" Value="Column" />
<Setter Property="HorizontalGridLinesBrush" Value="{DynamicResource MaterialDesignSelection}" />
<!--
<Setter Property="ColumnHeaderStyle" Value="{StaticResource MaterialDesignDataGridColumnHeader}"/>
<Setter Property="RowHeaderStyle" Value="{StaticResource MaterialDesignDataGridRowHeader}"/>
<Setter Property="RowStyle" Value="{StaticResource MaterialDesignDataGridRow}"/>
<Setter Property="CellStyle" Value="{StaticResource MaterialDesignDataGridCell}"/>
-->
<Setter Property="ColumnHeaderStyle" Value="{StaticResource DataGridColumnHeaderStyle}"/>
<Setter Property="RowHeaderStyle" Value="{StaticResource DataGridRowHeaderStyle}"/>
<Setter Property="RowStyle" Value="{StaticResource MaterialDesignDataGridRow}"/>
<Setter Property="CellStyle" Value="{StaticResource MaterialDesignDataGridCell}"/>
<!--<Setter Property="md:DataGridAssist.AutoGeneratedCheckBoxStyle" Value="{StaticResource MaterialDesignDataGridCheckBoxColumnStyle}" />
<Setter Property="md:DataGridAssist.AutoGeneratedEditingCheckBoxStyle" Value="{StaticResource MaterialDesignDataGridCheckBoxColumnEditingStyle}" />
<Setter Property="md:DataGridAssist.AutoGeneratedEditingTextStyle" Value="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}" />
-->
<Setter Property="md:DataGridAssist.EnableEditBoxAssist" Value="True" />
<!--<Setter Property="hc:DataGridAttach.TextColumnStyle" Value="{StaticResource DataGridTextColumnStyle}"/>-->
<Setter Property="hc:DataGridAttach.TextColumnStyle" Value="{StaticResource LSDataGridTextColumnStyle}"/>
<Setter Property="hc:DataGridAttach.EditingTextColumnStyle" Value="{StaticResource TextBoxBaseStyle}"/>
<Setter Property="hc:DataGridAttach.ComboBoxColumnStyle" Value="{StaticResource LSTextBlockComboBoxStyle}"/>
<Setter Property="hc:DataGridAttach.EditingComboBoxColumnStyle" Value="{StaticResource LSTextBlockComboBoxEditingStyle}"/>
<Setter Property="hc:DataGridAttach.CheckBoxColumnStyle" Value="{StaticResource CheckBoxBaseStyle}"/>
<Setter Property="hc:DataGridAttach.EditingCheckBoxColumnStyle" Value="{StaticResource CheckBoxBaseStyle}"/>
<Setter Property="hc:DataGridAttach.ApplyDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGrid}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
<ScrollViewer.Template>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Command="{x:Static DataGrid.SelectAllCommand}" Focusable="false" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
<Border BorderThickness="0 0 0 1" BorderBrush="{DynamicResource MaterialDesignDivider}"
Grid.Row="0" Grid.Column="1" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}">
<DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" />
</Border>
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" Grid.ColumnSpan="2"
Grid.Column="0" Grid.Row="1"/>
<ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
<Grid Grid.Column="1" Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
</Grid>
</Grid>
</ControlTemplate>
</ScrollViewer.Template>
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsGrouping" Value="true"/>
<Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</MultiTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
效果如下
其它
- 在datagrid中使用ComboboxColumn时会遇到一些绑定与显示的问题, 解决方案是分别定义ComboboxColumnSytle和ComboboxColumnEditingStyle, 此方法需要麻烦但是可以解决Combobox在DataGrid中的问题
<DataGridComboBoxColumn Header="节目类型">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=DataGrid}, Path=DataContext.Categories.Result}"/>
<Setter Property="DisplayMemberPath" Value="Title"/>
<Setter Property="SelectedValuePath" Value="Id"/>
<Setter Property="SelectedValue" Value="{Binding CategoryId, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
<Setter Property="SelectedItem" Value="{Binding CategoryModel, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=DataGrid}, Path=DataContext.Categories.Result}"/>
<Setter Property="DisplayMemberPath" Value="Title"/>
<Setter Property="SelectedValuePath" Value="Id"/>
<Setter Property="SelectedValue" Value="{Binding CategoryId, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
<Setter Property="SelectedItem" Value="{Binding CategoryModel, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
- 使用MaterialDesign的DataGridComboboxColumn
<md:DataGridComboBoxColumn Header="节目类型"
ItemsSourceBinding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=DataContext.Categories.Result}"
DisplayMemberPath="Title" SelectedValuePath="Id"
SelectedValueBinding="{Binding CategoryId, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedItemBinding="{Binding CategoryModel, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
Wpf之HandyControls与MaterialDesign混用之DataGrid的更多相关文章
- WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决
原文:WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决 如下图,在凭证编辑窗体中,有的单元格不需要数字,但如果录入数字后再删除,会触发数字验证,单元格显示红色框线,导致不能执行 ...
- WPF入门教程系列二十二——DataGrid示例(二)
DataGrid示例的后台代码 1) 通过Entity Framework 6.1 从数据库(本地数据库(local)/Test中的S_City表中读取城市信息数据,从S_ Province表中读取 ...
- WPF 格式化输出- IValueConverter接口的使用 datagrid列中的值转换显示
以前在用ASP.NET 做B/S系统时,可以方便地在GRIDVIEW DATAList等数据控件中,使用自定义的代码逻辑,比如 使用 <%# GetBalance(custID) %> 这 ...
- WPF学习笔记(1):DataGrid单元格实现逐键过滤功能
最近,开始学习WPF,其UI设计完全颠覆了传统的设计理念,为程序员提供了极大的自由发挥空间,让我为之惊叹,且为之着迷.然而,WPF在国内的热度却并不高,大部分贴子都是2012年以前的,出版的图书也很少 ...
- WPF备忘录(3)如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter
一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...
- WPF学习笔记(7):DataGrid中数字自定义格式显示
DataGrid中数据显示如下图,数据格式比较杂乱.希望达到以下要求:(1)所有数据保留两位小数:(2)超过1000的数字显示千分位:(3)如果数据为0,不显示. 首先想到用StringFormat进 ...
- WPF入门教程系列二十三——DataGrid示例(三)
DataGrid的选择模式 默认情况下,DataGrid 的选择模式为“全行选择”,并且可以同时选择多行(如下图所示),我们可以通过SelectionMode 和SelectionUnit 属性来修改 ...
- WPF设置DataGrid行内容高度自适应 与 TextBox/TextBlock内容高度自适应
WPF设置DataGrid行内容高度自适应 TextBox/TextBlock内容高度自适应 参考: DataGrid 控件中的调整大小选项: http://msdn.microsoft.com/ ...
- WPF:获取DataGrid控件单元格DataGridCell
转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...
随机推荐
- 网络安全从入门到精通(第一章-2)快速自建web安全测试环境
本文内容 动态语言 常见服务器脚本 动态语言可以做什么? 常见服务器环境快捷搭建软件 快速的搭网站的环境 CMS 1,动态语言:是对服务器行为的编程,这被称为服务器端脚本或服务器脚本. 2,常见服务器 ...
- SOFARPC模式下的Consul注册中心
Consul大家不陌生,就是和Zookeeper.Nacos一伙的,能够作为微服务基础架构的注册中心,算是比较成熟的组件,和Springcloud集成顺滑, 考虑到Eureka已经停止更新,所以有必要 ...
- 解析源码,彻底弄懂HashMap(持续更新中)
为啥突然想着看HashMap源码了? 无意间看到有人说HashMap能考验Java程序员的基本功,之前我作为面试官帮公司招人的时候偶尔问起HashMap,大部分人回答基本都会用,且多数仅停留在put, ...
- Vue-Cli4笔记
Vue-Cli4与Vue-Cli2区别浅谈 当时学习 Vue-Cli 的时候看的是 Vue-Cli2 的相关教程,当把 package.json 上传 github 的时候提醒有安全问题,于是准备使用 ...
- Jupyter的安装和基本使用
1. 安装Jupyter pip install jupyter 2. Jupyter的初次使用 # 进入虚拟环境 workon ai # 输入命令 jupyter notebook 本地notebo ...
- Linux开机启动程序rc.local
目录 1./etc/rc.local是/etc/rc.d/rc.local的软链接 2.rc.local文件的原始内容 3.rc.local文件的配置 4.应用经验 5.版权声明 在CentOS7中, ...
- hdu2732 最大流+拆点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2732 题目给定一个场景,有n*m个方格,每个方格代表一个柱子,一个柱子可以承受不同次数的跳跃,开始时图 ...
- 使用IDEA操作Hbase API 报错:org.apache.hadoop.hbase.client.RetriesExhaustedException的解决方法:
使用IDEA操作Hbase API 报错:org.apache.hadoop.hbase.client.RetriesExhaustedException的解决方法: 1.错误详情: Excepti ...
- Python datetime 时间处理
读入的时间数据是字符串格式,转换成datetime格式 data['time1'] = pd.to_datetime(data['time1'],format="%H:%M:%S:%f&qu ...
- 洛谷5026 Lycanthropy 差分套差分
题目链接 https://www.luogu.com.cn/problem/P5026 题意 在一个长度为m的序列中,每次给一个下标x,和一个权值v,然后从x-v*3到x-v*2单调递增,从x-v*2 ...