WPF (DataGridColumnHeader) to achieve self-righteous given column header style and bind data

Original: WPF (DataGridColumnHeader) to achieve self-righteous given column header style and bind data

 

Such a function is achieved 

Custom Styles Style column header in the data from the background binding 

This said head styles and style data binding head

Thinking

1) When the function would first like to achieve is to edit the column header style choose to use DataGridTextColumn edit DataGridColumnHeader style

Style is very simple to layout a good end of this

2) dynamic column does not require resurfacing so we are not fully using MVVM write directly back to the loop data has been here


  
  
  1. List< string> LS = new List< string>();
  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 < LS.Count; i++)
  11. {
  12. DataGridTextColumn dl = new DataGridTextColumn();
  13. dl.Header=LS[i];
  14. dataGrid.Columns.Add(dl);
  15. }
  16. }

3) the toughest data-bound data source header if there are only two months would not have so much trouble with ControlTemplate directly from the pattern TemplateBinding binding Content and tag on it

{TemplateBinding Content}

content = Header value in the tag to be used must of course add tag's value pattern to be bound {TemplateBinding tag} in the above for

But I need four items which need to boast more TemplateBinding this binding I checked want to extend the template but little information 

Workaround self-righteousness given control

First I show the lable control so I customized a lable write dependency property though a bit tedious it would be a stupid solution

1) defined Ms to obtain data and header data processing 

Data 2) to obtain the definition MyProperty Ms treatment bound to the Content property lable

tag 3) the use of the control itself to distinguish that lable

 Paste code:

Custom lable controls


  
  
  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. }

Front desk DataGridColumnHeader style


  
  
  1. <Style TargetType= "{x:Type DataGridColumnHeader}">
  2. <Setter Property= "VerticalContentAlignment" Value= "Center"/>
  3. <Setter Property= "Template">
  4. <Setter.Value>
  5. <ControlTemplate TargetType= "{x:Type DataGridColumnHeader}">
  6. <Grid HorizontalAlignment= "Left " Height= "Auto " VerticalAlignment= "Stretch " Width= "Auto " Background= "White " Margin= "0 ">
  7. <Grid.RowDefinitions>
  8. <RowDefinition Height= "* "/>
  9. </Grid.RowDefinitions>
  10. <Border BorderBrush= "Black " BorderThickness= "1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Grid.RowSpan= "1 ">
  11. <Grid HorizontalAlignment= "Stretch " Height= "Auto " Margin= "-1,0,0,0 " VerticalAlignment= "Stretch ">
  12. <Grid.RowDefinitions>
  13. <RowDefinition Height= "20* "/>
  14. <RowDefinition Height= "20* "/>
  15. <RowDefinition Height= "20* "/>
  16. <RowDefinition Height= "20* "/>
  17. </Grid.RowDefinitions>
  18. <Border BorderBrush= "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "1 ">
  19. <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 "/>
  20. </Border>
  21. <Border BorderBrush= "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto ">
  22. <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 "/>
  23. </Border>
  24. <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"/>
  25. <Border BorderBrush= "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0,0,0,0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "2 ">
  26. <TextBlock TextWrapping = "Wrap " Background= "#FFF9F9F9 " Text= "{Binding Path=Content,ElementName=lText}" HorizontalAlignment= "Stretch " Height= "35 " Margin= "0 " VerticalAlignment= "Stretch " Width= "82 "/>
  27. </Border>
  28. <Border BorderBrush= "Black " BorderThickness= "1,1,0,1 " HorizontalAlignment= "Stretch " Height= "Auto " Margin= "0 " VerticalAlignment= "Stretch " Width= "Auto " Grid.Row= "3 ">
  29. <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 "/>
  30. </Border>
  31. </Grid>
  32. </Border>
  33. </Grid>
  34. </ControlTemplate>
  35. </Setter.Value>
  36. </Setter>
  37. </Style>

 

Data binding involves two kinds of skills here

Is itself a binding property  

{Binding MyProperty, RelativeSource={RelativeSource self}}
 
 

The second is the binding properties of other controls

{Binding Path=Content,ElementName=lText}
 
 

There must be a better way to achieve this hope was a message to share learning

 

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12075390.html