简体   繁体   English

LoadingRow WPF的列标题

[英]Column header for LoadingRow WPF

How can I possibly put Header for my Loading Row. 我怎样才能在我的加载行标题中放置标题。 My datagrid looks like this: 我的数据网格看起来像这样:

在此处输入图片说明

I want to put a header above 1st Check, 2nd Check etc. 我想在第一张支票,第二张支票等上方放置标题。

The first column is being created using Datagrid_LoadingRow. 第一列是使用Datagrid_LoadingRow创建的。 Here is the code: 这是代码:

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";
    }
}

Here is my XAML for datagrid. 这是我针对datagrid的XAML。 May columns are in code using ItemSource. 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>

I want to make the header for the column where my row index is will be the same as the header for column Type, Amount, Edit and delete. 我想使行索引所在的列的标题与列类型,金额,编辑和删除的标题相同。 Can anyone help me out? 谁能帮我吗? I'm using c# wpf. 我正在使用c#wpf。 Thank you so much. 非常感谢。

If possible can anyone tell me how can I put the row index in a column specifically in column 1? 如果可能,谁能告诉我如何将行索引放在第1列中的一列中? Thank so much. 非常感谢。

here a full example showing how you can achieve a similar behavior without using the Loading Row event 这是一个完整的示例,展示了如何在不使用Loading Row事件的情况下实现类似的行为

XAML 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>

Code

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**

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM