原文:WPF (DataGridRowHeaderStyle)实现自义定行样式 并绑定数据
功能阐述
就上面那图片
刚开始 考虑使用 RowHeaderTemplate 来实现 发现总绑定不上数据 上官网查了一下
不支持上下文绑定 fffffffffffff
只能考虑样式了 经验不足 经验不足~
直接上前后台源码了
XAML
- <Window
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WpfApplication31.MainWindow"
- Title="MainWindow" Height="350" Width="525">
- <Window.Resources>
- <Style x:Key="DataGridRowHeaderStyle1" TargetType="{x:Type DataGridRowHeader}">
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
- <Grid Height="59.834" Width="207.908">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="20*"/>
- <ColumnDefinition Width="21*"/>
- <ColumnDefinition Width="77*"/>
- </Grid.ColumnDefinitions>
- <Grid Grid.Column="2" HorizontalAlignment="Stretch" Height="Auto" Margin="0" VerticalAlignment="Stretch">
- <Grid.RowDefinitions>
- <RowDefinition/>
- <RowDefinition/>
- </Grid.RowDefinitions>
- <Grid HorizontalAlignment="Stretch" Height="Auto" Margin="0" Grid.Row="1" VerticalAlignment="Stretch">
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <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">
- <Label Content="リーダー" HorizontalAlignment="Center" Height="Auto" Background="White" VerticalAlignment="Center" Margin="0"/>
- </Border>
- <Border BorderBrush="#FFBFBFBF" BorderThickness="0,0,1,1" Grid.ColumnSpan="1" HorizontalAlignment="Stretch" Height="Auto" VerticalAlignment="Stretch" Width="Auto" Background="White" Grid.Column="1">
- <Label Content="" HorizontalAlignment="Center" Height="Auto" Background="White" VerticalAlignment="Center" Margin="0"/>
- </Border>
- </Grid>
- <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">
- <Label Content="{Binding Name}" HorizontalAlignment="Center" Height="Auto" Background="White" VerticalAlignment="Center" Margin="0"/>
- </Border>
- </Grid>
- <Border BorderBrush="#FFBFBFBF" BorderThickness="1" Grid.ColumnSpan="1" HorizontalAlignment="Stretch" Height="Auto" Margin="0" VerticalAlignment="Stretch" Width="Auto" Background="White">
- <Label Content="{Binding Num}" HorizontalAlignment="Center" Height="Auto" Background="White" VerticalAlignment="Center" Margin="0"/>
- </Border>
- <Border BorderBrush="#FFBFBFBF" BorderThickness="0,1,1,1" Grid.ColumnSpan="1" HorizontalAlignment="Stretch" Height="Auto" VerticalAlignment="Stretch" Width="Auto" Background="White" Grid.Column="1">
- <Label Content="{Binding Class_}" HorizontalAlignment="Center" Height="Auto" Background="White" VerticalAlignment="Center" Margin="0"/>
- </Border>
- </Grid>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </Window.Resources>
- <Grid>
- <DataGrid x:Name="dataGrid" CanUserAddRows = "false" AutoGenerateColumns="False" Margin="10,10,0,0" RowHeaderStyle="{DynamicResource DataGridRowHeaderStyle1}" >
- <DataGrid.Columns>
- <DataGridTextColumn Header="姓名" Binding="{Binding Name}"/>
- <DataGridTextColumn Header="年龄" Binding="{Binding Class_}"/>
- </DataGrid.Columns>
- </DataGrid>
- </Grid>
- </Window>
后台代码
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace WpfApplication31
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- dataGrid.ItemsSource = GetNameData();
- }
- ObservableCollection<NameList> listName = new ObservableCollection<NameList>();
- private ObservableCollection<NameList> GetNameData()
- {
- listName.Add(new NameList("市川 賞子", "リーダー", "B", 1));
- listName.Add(new NameList("石田", "リーダー", "C", 2));
- listName.Add(new NameList("安达 鮎美", "リーダー", "C", 3));
- return listName;
- }
- }
- public class NameList : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- public NameList(string name, string jOb, string class_, int num) { Name = name; Class_ = class_; JOb = jOb; Num = num; }
- private string name;
- public string Name
- {
- get { return name; }
- set
- {
- name = value;
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs("Name"));
- }
- }
- }
- private int num;
- public int Num
- {
- get { return num; }
- set
- {
- num = value;
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs("Num"));
- }
- }
- }
- private string class_;
- public string Class_
- {
- get { return class_; }
- set
- {
- class_ = value;
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs("Class_"));
- }
- }
- }
- private string jOb;
- public string JOb
- {
- get { return jOb; }
- set
- {
- jOb = value;
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs("JOb"));
- }
- }
- }
- }
- }
原文地址:https://www.cnblogs.com/lonelyxmas/p/12075388.html
时间: 2024-11-08 12:03:50