WPF (DataGridColumnHeader)实现自义定列头样式 并绑定数据
原文:WPF (DataGridColumnHeader)实现自义定列头样式 并绑定数据

实现功能是这样的
自定义列头 样式 样式里的 数据来源于后台绑定
这篇就说头样式 和头样式数据绑定
思路
1)实现功能的时候 首先想的是编辑列头样式 选择使用DataGridTextColumn 编辑 DataGridColumnHeader 样式
样式很简单 就布局好了 这段结束
2)动态列 没有要求换肤 所以就没有完全使用MVVM 直接写后台循环 到这里数据有了
-
List<string> LS = new List<string>();
-
-
public void addColumn()
-
{
-
LS.Add("表下カップ綿天竺仮縫い_37s_C_1");
-
LS.Add("上カップマーキしつけ_28s_C_2");
-
LS.Add("上下カップ接ぎ_33s_C_3");
-
LS.Add("上下カップ押え_62s_B_4");
-
LS.Add("カップ脇しつけ_14s_B_5");
-
LS.Add("表上カップレース端押さえ_41s_B_6");
-
-
for (int i = 0; i < LS.Count; i++)
-
{
-
DataGridTextColumn dl = new DataGridTextColumn();
-
dl.Header=LS[i];
-
-
dataGrid.Columns.Add(dl);
-
}
-
-
}
3)最难的数据绑定 数据来源 header 如果有只有俩个 就不用那么麻烦 直接在样式里ControlTemplate 中用TemplateBinding 绑定 Content 和tag 就可以
{TemplateBinding Content}
content = Header 里的值 当然 要使用tag 就要在上面的for 里加上tag的值 样式里 需要 绑定{TemplateBinding tag}
但是 我的项目需要4个 这就需要夸越TemplateBinding 这个绑定 我查了一下 想扩展template 但是资料太少
解决方法 自义定控件
首先我显示的控件是lable 所以 我自定义了一个lable 写 依赖属性 虽然有点繁琐 也算是一个比较笨的解决方案
1)定义 Ms 来获得header的数据 并处理数据
2)定义MyProperty 来获得Ms处理后的数据 绑定到 lable 的 Content 属性
3)使用控件本身的tag 来区分那个lable
贴码:
自定义的lable控件
-
public class LableColumn : Label
-
{
-
public LableColumn()
-
: base()
-
{
-
}
-
-
//获得值
-
public string Ms
-
{
-
get { return (string)GetValue(MsProperty); }
-
set { SetValue(MsProperty, value); }
-
}
-
-
// Using a DependencyProperty as the backing store for ms. This enables animation, styling, binding, etc...
-
public static readonly DependencyProperty MsProperty =
-
DependencyProperty.Register("Ms", typeof(string), typeof(LableColumn), new FrameworkPropertyMetadata("", Onshow));
-
-
-
-
//用于绑定的值
-
public string MyProperty
-
{
-
get { return (string)GetValue(MyPropertyProperty); }
-
set { SetValue(MyPropertyProperty, value); }
-
}
-
-
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
-
public static readonly DependencyProperty MyPropertyProperty =
-
DependencyProperty.Register("MyProperty", typeof(string), typeof(LableColumn), new PropertyMetadata(""));
-
-
-
-
-
-
private static void Onshow(DependencyObject d, DependencyPropertyChangedEventArgs e)
-
{
-
LableColumn l = d as LableColumn;
-
-
if (l.Ms != null && l.Ms != "")
-
{
-
String[] strarr = l.Ms.ToString().Split(new string[] { "_" }, StringSplitOptions.None);
-
-
if (l.Tag.Equals("1"))
-
{
-
l.MyProperty= strarr[3];
-
}
-
else if (l.Tag.Equals("2"))
-
{
-
l.MyProperty = strarr[0];
-
-
}
-
else if (l.Tag.Equals("3"))
-
{ l.MyProperty= strarr[2] ;
-
}
-
else if (l.Tag.Equals("4"))
-
{
-
l.MyProperty= strarr[1];
-
}
-
}
-
}
-
-
-
}
前台的DataGridColumnHeader 样式
-
<Style TargetType="{x:Type DataGridColumnHeader}">
-
<Setter Property="VerticalContentAlignment" Value="Center"/>
-
<Setter Property="Template">
-
<Setter.Value>
-
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
-
-
<Grid HorizontalAlignment= "Left " Height= "Auto " VerticalAlignment= "Stretch " Width= "Auto " Background= "White " Margin= "0 ">
-
<Grid.RowDefinitions>
-
<RowDefinition Height= "* "/>
-
</Grid.RowDefinitions>
-
<Border BorderBrush= "Black " BorderThickness= "1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Grid.RowSpan= "1 ">
-
<Grid HorizontalAlignment= "Stretch " Height= "Auto " Margin= "-1,0,0,0 " VerticalAlignment= "Stretch ">
-
<Grid.RowDefinitions>
-
<RowDefinition Height= "20* "/>
-
<RowDefinition Height= "20* "/>
-
<RowDefinition Height= "20* "/>
-
<RowDefinition Height= "20* "/>
-
</Grid.RowDefinitions>
-
<Border BorderBrush= "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "1 ">
-
<local:LableColumn Tag="3" Content="{Binding RelativeSource={RelativeSource self},Path=MyProperty}" Ms="{TemplateBinding Content}" Background= "#FFF9F9F9 " HorizontalAlignment= "Stretch " Height= "25 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
-
</Border>
-
<Border BorderBrush= "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto ">
-
<local:LableColumn Tag="1" Content="{Binding MyProperty, RelativeSource={RelativeSource self}}" Ms="{TemplateBinding Content}" Background= "#FFF9F9F9 " HorizontalAlignment= "Stretch " Height= "25 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
-
</Border>
-
<local:LableColumn x:Name="lText" Tag="2" Content="{Binding MyProperty, RelativeSource={RelativeSource self}}" Ms="{TemplateBinding Content}" Background= "#FFF9F9F9 " HorizontalAlignment= "Stretch " Height= "35 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 " Visibility="Collapsed"/>
-
-
<Border BorderBrush= "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0,0,0,0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "2 ">
-
<TextBlock TextWrapping = "Wrap " Background= "#FFF9F9F9 " Text="{Binding Path=Content,ElementName=lText}" HorizontalAlignment= "Stretch " Height= "35 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
-
-
</Border>
-
<Border BorderBrush= "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "3 ">
-
<local:LableColumn Tag="4" Content="{Binding MyProperty, RelativeSource={RelativeSource self}}" Ms="{TemplateBinding Content}" Background= "#FFF9F9F9 " HorizontalAlignment= "Stretch " Height= "35 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
-
</Border>
-
</Grid>
-
</Border>
-
</Grid>
-
-
</ControlTemplate>
-
</Setter.Value>
-
</Setter>
-
</Style>
数据绑定的技能 这边涉及到俩种
一个是绑定自身属性
{Binding MyProperty, RelativeSource={RelativeSource self}}
第二是绑定其他控件属性
{Binding Path=Content,ElementName=lText}
肯定有更好的方法来实现 这个功能 希望有人留言 得以分享学习

WPF (DataGridColumnHeader)实现自义定列头样式 并绑定数据的更多相关文章
- WPF (DataGridRowHeaderStyle)实现自义定行样式 并绑定数据
原文:WPF (DataGridRowHeaderStyle)实现自义定行样式 并绑定数据 功能阐述 就上面那图片 刚开始 考虑使用 RowHeaderTemplate 来实现 发现总绑定不上数据 ...
- WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)对象绑定
原文:WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)对象绑定 WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件) 上面的 ...
- WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)
原文:WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件) 因为项目需要 要实现这个~ 怎么实现画红框内容部分 田字格和上面Textbox 属于一个自定义控件 大 ...
- WPF 用 DataTemplate 合并DataGrid列表列头<类似报表设计>及行头列头样式 - 学习
WPF中 DataGrid 列头合并,类似于报表设计.效果图如下↓ 1.新建一个WPF项目WpfApplication1,新建一个窗体DataGridTest,前台代码如下: <Window x ...
- Extjs 动态修改gridPanel列头信息以及store数据的方法
1 /*******************************checkbox按钮 历史报警信息**************************************/ var check ...
- dataTable 加了竖向滚动条导致列头样式错位的问题 / 亲测可用,不好用你打我,用好了记得点推荐
tab在没有显示之前,容器是没有高度宽度的,而dt在自动计算高度和宽度时是获取的外部容器的高度和宽度,当切换tab时,dt获取不到这个高度宽度,导致列头都挤在一起,是用下面代码解决此问题 $('a[d ...
- WPF报表自定义通用可筛选列头-WPF特工队内部资料
由于项目需要制作一个可通用的报表多行标题,且可实现各种类型的内容显示,包括文本.输入框.下拉框.多选框等(自定的显示内容可自行扩展),并支持参数绑定转换,效果如下: 源码结构 ColumnItem类: ...
- 设置DatagridView的列头样式
设置DataGridView.ColumnHeaderDefaultCellStyle的BackColor属性会发现没有效果.这是因为在启动了可视样式的时候,BackColor和ForeColor的值 ...
- WPF Datagrid 动态生成列 并绑定数据
原文:WPF Datagrid 动态生成列 并绑定数据 说的是这里 因为列头是动态加载的 (后台for循环 一会能看到代码) 数据来源于左侧列 左侧列数据源 当然num1 属于临时的dome使用 可 ...
随机推荐
- SSM框架之SpringMVC(4)返回值类型及响应数据类型
SpringMVC(4)返回值类型及响应数据类型 1. 返回值分类 1.1. 返回字符串 Controller方法返回字符串可以指定逻辑视图的名称,根据视图解析器为物理视图的地址. @RequestM ...
- vue引入ElementUI库
element国内网站:https://element.eleme.cn/#/zh-CN 引入ElementUI命令:npm install element-ui --save (网速不好用cnp ...
- make和new关键字的区别及实现原
new 和 make 是两个内置函数,主要用来创建并分配类型的内存.在我们定义变量的时候,可能会觉得有点迷惑,不知道应该使用哪个函数来声明变量,其实他们的规则很简单, new 只分配内存, make ...
- bayaim_Centos7.6_mysql源码5.7-多my.cnf_20190424.txt
用户名/密码mysql/mysql 一.安装mysql: 位置位于 /data/mysql 如果遇到依赖,无法删除,使用 rpm -e --nodeps <包的名字> 不检查依赖,直接删除 ...
- 12. java ArrayList类
一.ArrayList定义 java.util.ArrayList是大小可变的数组的实现,存储在内的数据成为元素.此类提供一些方法来操作内部存储的元素.ArrayList中可不断添加元素,其大小也 ...
- mysql分组报错Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
环境:Server version: 5.7.27-0 ubuntu 18.04.1 (Ubuntu) 执行分组语句:select * from pet group by owner;时报错: mys ...
- java对 zip文件的压缩和解压(ant解决中文乱码)
说明: 1.对于压缩的文件,当文件名称是中文时,若使用JDK API中自带的类(java.util.zip.ZipEntry; java.util.zip.ZipOutputStream;)进行压缩, ...
- ADB常用命令(一)
转自:https://blog.csdn.net/qq_26552691/article/details/81348222 一.操作前请确认电脑上已配置好ADB环境.可在CMD命令行输入adb,如果出 ...
- C++ std::vector emplace_back 优于 push_back 的理由
#include <iostream> #include <vector> #include <chrono> #include <windows.h> ...
- 使用过Redis,我竟然还不知道Rdb
目录 使用过Redis,那就先说说使用过那些场景吧 Rdb文件是什么,它是干什么的 分析工具 小结 联想 推荐阅读 使用过Redis,那就先说说使用过那些场景吧 字符串缓存 //举例 $redis-& ...