我怎样才能为我的装载行添加标题。我的datagrid看起来像这样:
我想在第一张支票,第二张支票等上面加上一个标题。
使用Datagrid_LoadingRow创建第一列。这是代码:
void datagrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
if (counter.CheckCount > 1)
{
if ((e.Row.GetIndex() + 1) == 1)
CheckNumber = "1st";
else if ((e.Row.GetIndex() + 1) == 2)
CheckNumber = "2nd";
else if ((e.Row.GetIndex() + 1) == 3)
CheckNumber = "3rd";
else
CheckNumber = (e.Row.GetIndex() + 1).ToString() + "th";
e.Row.Header = CheckNumber + " Check";
}
else
{
e.Row.Header = (e.Row.GetIndex() + 1).ToString() + " Check";
}
}
这是我的数据网格XAML。 May列使用ItemSource代码。
<DataGrid Name="datagrid" LoadingRow="datagrid_LoadingRow" SelectionMode="Single" Margin="12,140,32,0" Height="310" VerticalAlignment="Top" Width="746"
BorderBrush="#FF444444" BorderThickness="1" RowHeight="30" IsEnabled="True" IsReadOnly="True" FontSize="12" ColumnWidth="*" FontFamily="Arial"
HorizontalAlignment="Center" AutoGeneratingColumn="dgCheckSummary_AutoGeneratingColumn">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="{StaticResource DataGridHeaderBackgroundBrush}" />
<Setter Property="FontSize" Value="13" />
<Setter Property="Padding" Value="5" />
<Setter Property="Foreground" Value="White" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
我想让我的行索引所在的列的标题与列Type,Amount,Edit和delete的标题相同。谁能帮我吗?我正在使用c#wpf。非常感谢你。
如果可能,任何人都可以告诉我如何将行索引专门放在第1列中的列中?非常感谢。
答案 0 :(得分:0)
这里有一个完整的示例,说明如何在不使用Loading Row
事件
<强> XAML 强>
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication5"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<local:ConvertItemToIndex x:Key="IndexConverter"/>
</Window.Resources>
<Grid>
<DataGrid Name="DG1" ItemsSource="{Binding}" CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="True">
<!--Bind the Content property of the RowHeaderStyle to the Converter to create numbered rows-->
<DataGrid.RowHeaderStyle>
<Style TargetType="{x:Type DataGridRowHeader}">
<Setter Property="Content" Value="{Binding Converter={StaticResource IndexConverter}}" />
</Style>
</DataGrid.RowHeaderStyle>
</DataGrid>
</Grid>
</Window>
<强>代码强>
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;
namespace WpfApplication5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<MyData> Datas = new ObservableCollection<MyData>();
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 100; i++)
{
Datas.Add(new MyData() { Prop1 = "test prop" + i, Prop2 = "test prop" + i, });
}
this.DataContext = Datas;
}
}
public class MyData
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
public class ConvertItemToIndex : IValueConverter
{
private static int checkNumber;
#region IValueConverter Members
//Convert the Item to an Index
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
return "Check " + ++checkNumber;
}
catch (Exception e)
{
throw new NotImplementedException(e.Message);
}
}
//One way binding, so ConvertBack is not implemented
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
**Result**