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

2021-01-23 06:13

阅读:686

标签:null   ima   eve   return   bsp   nim   tail   rowspan   int   

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

 

技术图片

实现功能是这样的 

自定义列头 样式 样式里的 数据来源于后台绑定 

这篇就说头样式 和头样式数据绑定

思路

1)实现功能的时候 首先想的是编辑列头样式 选择使用DataGridTextColumn 编辑 DataGridColumnHeader 样式

样式很简单 就布局好了 这段结束

2)动态列 没有要求换肤 所以就没有完全使用MVVM 直接写后台循环   到这里数据有了

  1. Liststring> LS = new Liststring>();
  2. public void addColumn()
  3. {
  4. LS.Add("表下カップ綿天竺仮縫い_37s_C_1");
  5. LS.Add("上カップマーキしつけ_28s_C_2");
  6. LS.Add("上下カップ接ぎ_33s_C_3");
  7. LS.Add("上下カップ押え_62s_B_4");
  8. LS.Add("カップ脇しつけ_14s_B_5");
  9. LS.Add("表上カップレース端押さえ_41s_B_6");
  10. for (int i = 0; i
  11. {
  12. DataGridTextColumn dl = new DataGridTextColumn();
  13. dl.Header=LS[i];
  14. dataGrid.Columns.Add(dl);
  15. }
  16. }
  17. 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控件

    1. public class LableColumn : Label
    2. {
    3. public LableColumn()
    4. : base()
    5. {
    6. }
    7. //获得值
    8. public string Ms
    9. {
    10. get { return (string)GetValue(MsProperty); }
    11. set { SetValue(MsProperty, value); }
    12. }
    13. // Using a DependencyProperty as the backing store for ms. This enables animation, styling, binding, etc...
    14. public static readonly DependencyProperty MsProperty =
    15. DependencyProperty.Register("Ms", typeof(string), typeof(LableColumn), new FrameworkPropertyMetadata("", Onshow));
    16. //用于绑定的值
    17. public string MyProperty
    18. {
    19. get { return (string)GetValue(MyPropertyProperty); }
    20. set { SetValue(MyPropertyProperty, value); }
    21. }
    22. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
    23. public static readonly DependencyProperty MyPropertyProperty =
    24. DependencyProperty.Register("MyProperty", typeof(string), typeof(LableColumn), new PropertyMetadata(""));
    25. private static void Onshow(DependencyObject d, DependencyPropertyChangedEventArgs e)
    26. {
    27. LableColumn l = d as LableColumn;
    28. if (l.Ms != null && l.Ms != "")
    29. {
    30. String[] strarr = l.Ms.ToString().Split(new string[] { "_" }, StringSplitOptions.None);
    31. if (l.Tag.Equals("1"))
    32. {
    33. l.MyProperty= strarr[3];
    34. }
    35. else if (l.Tag.Equals("2"))
    36. {
    37. l.MyProperty = strarr[0];
    38. }
    39. else if (l.Tag.Equals("3"))
    40. { l.MyProperty= strarr[2] ;
    41. }
    42. else if (l.Tag.Equals("4"))
    43. {
    44. l.MyProperty= strarr[1];
    45. }
    46. }
    47. }
    48. }

    前台的DataGridColumnHeader 样式

    1. "VerticalContentAlignment" Value="Center"/>
    2. "Template">
    3. "{x:Type DataGridColumnHeader}">
    4. "Left " Height= "Auto " VerticalAlignment= "Stretch " Width= "Auto " Background= "White " Margin= "0 ">
    5. "* "/>
    6. "Black " BorderThickness= "1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Grid.RowSpan= "1 ">
    7. "Stretch " Height= "Auto " Margin= "-1,0,0,0 " VerticalAlignment= "Stretch ">
    8. "20* "/>
    9. "20* "/>
    10. "20* "/>
    11. "20* "/>
    12. "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "1 ">
    13. "3" Content="{Binding RelativeSource={RelativeSource self},Path=MyProperty}" Ms="{TemplateBinding Content}" Background= "#FFF9F9F9 " HorizontalAlignment= "Stretch " Height= "25 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
    14. "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto ">
    15. "1" Content="{Binding MyProperty, RelativeSource={RelativeSource self}}" Ms="{TemplateBinding Content}" Background= "#FFF9F9F9 " HorizontalAlignment= "Stretch " Height= "25 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
    16. "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"/>
    17. "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0,0,0,0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "2 ">
    18. "Wrap " Background= "#FFF9F9F9 " Text="{Binding Path=Content,ElementName=lText}" HorizontalAlignment= "Stretch " Height= "35 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
    19. "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "3 ">
    20. "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


评论


亲,登录后才可以留言!

推荐文章

最新文章

置顶文章