WPF (DataGridRowHeaderStyle)实现自义定行样式 并绑定数据

原文:WPF (DataGridRowHeaderStyle)实现自义定行样式 并绑定数据

功能阐述

就上面那图片

刚开始 考虑使用 RowHeaderTemplate 来实现  发现总绑定不上数据  上官网查了一下

不支持上下文绑定  fffffffffffff

只能考虑样式了  经验不足 经验不足~

直接上前后台源码了

XAML


  1. <Window
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WpfApplication31.MainWindow"
  5. Title="MainWindow" Height="350" Width="525">
  6. <Window.Resources>
  7. <Style x:Key="DataGridRowHeaderStyle1" TargetType="{x:Type DataGridRowHeader}">
  8. <Setter Property="Template">
  9. <Setter.Value>
  10. <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
  11. <Grid Height="59.834" Width="207.908">
  12. <Grid.ColumnDefinitions>
  13. <ColumnDefinition Width="20*"/>
  14. <ColumnDefinition Width="21*"/>
  15. <ColumnDefinition Width="77*"/>
  16. </Grid.ColumnDefinitions>
  17. <Grid Grid.Column="2" HorizontalAlignment="Stretch" Height="Auto" Margin="0" VerticalAlignment="Stretch">
  18. <Grid.RowDefinitions>
  19. <RowDefinition/>
  20. <RowDefinition/>
  21. </Grid.RowDefinitions>
  22. <Grid HorizontalAlignment="Stretch" Height="Auto" Margin="0" Grid.Row="1" VerticalAlignment="Stretch">
  23. <Grid.ColumnDefinitions>
  24. <ColumnDefinition/>
  25. <ColumnDefinition/>
  26. </Grid.ColumnDefinitions>
  27. <Border BorderBrush="#FFBFBFBF" BorderThickness="0,0,1,1" Grid.ColumnSpan="1" HorizontalAlignment="Stretch" Height="Auto" VerticalAlignment="Stretch" Width="Auto" Background="White" Grid.Column="0" Margin="0">
  28. <Label Content="リーダー" HorizontalAlignment="Center" Height="Auto" Background="White" VerticalAlignment="Center" Margin="0"/>
  29. </Border>
  30. <Border BorderBrush="#FFBFBFBF" BorderThickness="0,0,1,1" Grid.ColumnSpan="1" HorizontalAlignment="Stretch" Height="Auto" VerticalAlignment="Stretch" Width="Auto" Background="White" Grid.Column="1">
  31. <Label Content="" HorizontalAlignment="Center" Height="Auto" Background="White" VerticalAlignment="Center" Margin="0"/>
  32. </Border>
  33. </Grid>
  34. <Border BorderBrush="#FFBFBFBF" BorderThickness="0,1,1,1" Grid.ColumnSpan="1" HorizontalAlignment="Stretch" Height="Auto" VerticalAlignment="Stretch" Width="Auto" Background="White" Grid.Column="0" Margin="0">
  35. <Label Content="{Binding Name}" HorizontalAlignment="Center" Height="Auto" Background="White" VerticalAlignment="Center" Margin="0"/>
  36. </Border>
  37. </Grid>
  38. <Border BorderBrush="#FFBFBFBF" BorderThickness="1" Grid.ColumnSpan="1" HorizontalAlignment="Stretch" Height="Auto" Margin="0" VerticalAlignment="Stretch" Width="Auto" Background="White">
  39. <Label Content="{Binding Num}" HorizontalAlignment="Center" Height="Auto" Background="White" VerticalAlignment="Center" Margin="0"/>
  40. </Border>
  41. <Border BorderBrush="#FFBFBFBF" BorderThickness="0,1,1,1" Grid.ColumnSpan="1" HorizontalAlignment="Stretch" Height="Auto" VerticalAlignment="Stretch" Width="Auto" Background="White" Grid.Column="1">
  42. <Label Content="{Binding Class_}" HorizontalAlignment="Center" Height="Auto" Background="White" VerticalAlignment="Center" Margin="0"/>
  43. </Border>
  44. </Grid>
  45. </ControlTemplate>
  46. </Setter.Value>
  47. </Setter>
  48. </Style>
  49. </Window.Resources>
  50. <Grid>
  51. <DataGrid x:Name="dataGrid" CanUserAddRows = "false" AutoGenerateColumns="False" Margin="10,10,0,0" RowHeaderStyle="{DynamicResource DataGridRowHeaderStyle1}" >
  52. <DataGrid.Columns>
  53. <DataGridTextColumn Header="姓名" Binding="{Binding Name}"/>
  54. <DataGridTextColumn Header="年龄" Binding="{Binding Class_}"/>
  55. </DataGrid.Columns>
  56. </DataGrid>
  57. </Grid>
  58. </Window>

后台代码


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace WpfApplication31
  18. {
  19. /// <summary>
  20. /// MainWindow.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. dataGrid.ItemsSource = GetNameData();
  28. }
  29. ObservableCollection<NameList> listName = new ObservableCollection<NameList>();
  30. private ObservableCollection<NameList> GetNameData()
  31. {
  32. listName.Add(new NameList("市川 賞子", "リーダー", "B", 1));
  33. listName.Add(new NameList("石田", "リーダー", "C", 2));
  34. listName.Add(new NameList("安达 鮎美", "リーダー", "C", 3));
  35. return listName;
  36. }
  37. }
  38. public class NameList : INotifyPropertyChanged
  39. {
  40. public event PropertyChangedEventHandler PropertyChanged;
  41. public NameList(string name, string jOb, string class_, int num) { Name = name; Class_ = class_; JOb = jOb; Num = num; }
  42. private string name;
  43. public string Name
  44. {
  45. get { return name; }
  46. set
  47. {
  48. name = value;
  49. if (PropertyChanged != null)
  50. {
  51. PropertyChanged(this, new PropertyChangedEventArgs("Name"));
  52. }
  53. }
  54. }
  55. private int num;
  56. public int Num
  57. {
  58. get { return num; }
  59. set
  60. {
  61. num = value;
  62. if (PropertyChanged != null)
  63. {
  64. PropertyChanged(this, new PropertyChangedEventArgs("Num"));
  65. }
  66. }
  67. }
  68. private string class_;
  69. public string Class_
  70. {
  71. get { return class_; }
  72. set
  73. {
  74. class_ = value;
  75. if (PropertyChanged != null)
  76. {
  77. PropertyChanged(this, new PropertyChangedEventArgs("Class_"));
  78. }
  79. }
  80. }
  81. private string jOb;
  82. public string JOb
  83. {
  84. get { return jOb; }
  85. set
  86. {
  87. jOb = value;
  88. if (PropertyChanged != null)
  89. {
  90. PropertyChanged(this, new PropertyChangedEventArgs("JOb"));
  91. }
  92. }
  93. }
  94. }
  95. }

原文地址:https://www.cnblogs.com/lonelyxmas/p/12075388.html

时间: 2024-11-08 12:03:50

WPF (DataGridRowHeaderStyle)实现自义定行样式 并绑定数据的相关文章

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

原文:WPF (DataGridColumnHeader)实现自义定列头样式 并绑定数据 实现功能是这样的 自定义列头 样式 样式里的 数据来源于后台绑定 这篇就说头样式 和头样式数据绑定 思路 1)实现功能的时候 首先想的是编辑列头样式 选择使用DataGridTextColumn 编辑 DataGridColumnHeader 样式 样式很简单 就布局好了 这段结束 2)动态列 没有要求换肤 所以就没有完全使用MVVM 直接写后台循环   到这里数据有了 List<string> LS =

WPF ListView点击删除某一行并获取绑定数据

最近在开发WPF程序时遇到一个问题,在gridview中希望实现在每一行最后添加一个删除的按钮,但是发现点击每行的button时只会触发button的点击事件,并没有选中这一行,此时调用list.SelectedItem时无法得到对应的绑定数据. UI.xaml<ListView x:Name="list" Height="494" Width="1121" FontSize="16" ><ListView.

WPF Datagrid 动态生成列 并绑定数据

原文:WPF Datagrid 动态生成列 并绑定数据 说的是这里 因为列头是动态加载的 (后台for循环 一会能看到代码) 数据来源于左侧列 左侧列数据源 当然num1 属于临时的dome使用  可以用ObservableCollection集合代表 动态创建属性 WPF 动态生成对象属性 (dynamic) ObservableCollection<NameList> listName = new ObservableCollection<NameList>(); privat

jquery easyui datagrid设置行样式 不可删除某行

rowStyler: function (index,row) { if (parseInt(row.ksrs) > 0) { return 'color:red'; } }, onLoadSuccess:function(data){ var rows = data.rows; for (var j = 0; j < rows.length; j++) { if (parseInt(rows[j]["ksrs"]) > 0) { $(".datagrid-

C# DevExpress_gridControl 行号行样式

#region 行号 /// <summary> /// 行号 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Gr

WPF后台设置xaml控件的样式System.Windows.Style

WPF后台设置xaml控件的样式System.Windows.Style 摘-自 :感谢 作者: IT小兵   http://3w.suchso.com/projecteac-tual/wpf-zhishi-houtai-shezhi-style.html Style myStyle = (Style)this.FindResource("TabItemStyle");//TabItemStyle 这个样式是引用的资源文件中的样式名称 静态资源在第一次编译后即确定其对象或值,之后不能对

Wpf DataGrid动态添加列,行数据(二)

这是第二中方法,可直接绑定,我这里只是做出了一种思路,并不是最完美. 这里注意一下,因为我里面引用了MVVMLight,所以可能代码不是复制过去就能用了的. 样式也是,所以复制过去看不是我贴出来的界面这也不奇怪.代码: <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions

css 设置奇数行偶数行样式

.list ul li:nth-child(even){ ...}    //li的偶数行样式 .list ul li:nth-child(odd){ ...}    //li的奇数行样式 .list ul li:nth-child(1){ top:0;}       //第一个li.list ul li:nth-child(2){ top:30px;}    //第二个li 部分情况下的解决方案 <style>  .list li:nth-of-type(odd){ margin-left:

WPF DataGrid 双击行 获得绑定数据

原文:WPF DataGrid 双击行 获得绑定数据 1)增加事件 2)增加对象获取 1)事件代码 Datagrid 增加事件 MouseDoubleClick="dataGrid_MouseDoubleClick" private void dataGrid_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e) { DataGrid datagrid = sender as DataG