for (int i = 0; i
DataGridTextColumn dl = new DataGridTextColumn();
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
-
-
-
-
-
-
-
-
-
-
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));
-
-
-
-
-
-
-
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);
-
-
-
-
-
-
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"))
-
-
-
-
-
-
-
-
前台的DataGridColumnHeader 样式
-
-
"VerticalContentAlignment" Value="Center"/>
-
-
-
"{x:Type DataGridColumnHeader}">
-
-
"Left " Height= "Auto " VerticalAlignment= "Stretch " Width= "Auto " Background= "White " Margin= "0 ">
-
-
-
-
"Black " BorderThickness= "1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Grid.RowSpan= "1 ">
-
"Stretch " Height= "Auto " Margin= "-1,0,0,0 " VerticalAlignment= "Stretch ">
-
-
-
-
-
-
-
"Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "1 ">
-
"3" Content="{Binding RelativeSource={RelativeSource self},Path=MyProperty}" Ms="{TemplateBinding Content}" Background= "#FFF9F9F9 " HorizontalAlignment= "Stretch " Height= "25 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
-
-
"Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto ">
-
"1" Content="{Binding MyProperty, RelativeSource={RelativeSource self}}" Ms="{TemplateBinding Content}" Background= "#FFF9F9F9 " HorizontalAlignment= "Stretch " Height= "25 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
-
-
"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"/>
-
-
"Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0,0,0,0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "2 ">
-
"Wrap " Background= "#FFF9F9F9 " Text="{Binding Path=Content,ElementName=lText}" HorizontalAlignment= "Stretch " Height= "35 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
-
-
-
"Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "3 ">
-
"4" Content="{Binding MyProperty, RelativeSource={RelativeSource self}}" Ms="{TemplateBinding Content}" Background= "#FFF9F9F9 " HorizontalAlignment= "Stretch " Height= "35 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
-
-
-
-
-
-
-
-
-
数据绑定的技能 这边涉及到俩种
一个是绑定自身属性
{Binding MyProperty, RelativeSource={RelativeSource self}}
第二是绑定其他控件属性
{Binding Path=Content,ElementName=lText}
肯定有更好的方法来实现 这个功能 希望有人留言 得以分享学习
WPF (DataGridColumnHeader)实现自义定列头样式 并绑定数据
标签:null ima eve return bsp nim tail rowspan int
原文地址:https://www.cnblogs.com/lonelyxmas/p/12075390.html