如何在 DevExpress 中更改 Master-Detail 网格控件上的 header 列?
How to change the column header on a Master-Detail Grid Control in DevExpress?
我想知道如何在 Master-Detail 网格控件中更改列 header 的名称。我已经使用代码
成功更改了 Master header 的名称
<dxg:GridColumn FieldName="SaleID" DisplayMemberBinding="{Binding SaleID}" Visible="False"/>
然而,当我尝试将相同的应用到我的详细信息时:
<dxg:GridColumn FieldName="Item Name" DisplayMemberBinding="{Binding SaleItemMainName}" HorizontalHeaderContentAlignment="Center"/>
它添加了另一列而不是重命名 header 所以我得到了两列具有 SaleItemMainName 的值,它们的 header 被称为:
- 项目名称
- 促销品主要名称
作为参考,这是我的网格控件的 XAML 代码:
<dxg:GridControl Grid.Row="1" x:Name="salePendingGC" AutoGenerateColumns="AddNew" Margin="20" Grid.ColumnSpan="2" Visibility="Hidden" >
<dxg:GridControl.View>
<dxg:TableView x:Name="salePendingTable" DetailHeaderContent="Sales" AutoWidth="True" ShowGroupPanel="False" RowMinHeight="40">
<dxg:TableView.RowStyle>
<Style TargetType="{x:Type dxg:RowControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectionState}" Value="None">
<Setter Property="Background" Value="#FFF2DEDE"/>
</DataTrigger>
</Style.Triggers>
</Style>
</dxg:TableView.RowStyle>
</dxg:TableView>
</dxg:GridControl.View>
<dxg:GridControl.DetailDescriptor>
<dxg:DataControlDetailDescriptor ItemsSourcePath="SaleItemMain">
<dxg:GridControl AutoGenerateColumns="AddNew">
<dxg:GridControl.View>
<dxg:TableView DetailHeaderContent="SaleItemMain" AutoWidth="True" ShowGroupPanel="False" ShowColumnHeaders="True" RowMinHeight="35" ShowVerticalLines="False" ShowHorizontalLines="False">
<dxg:TableView.RowStyle>
<Style TargetType="{x:Type dxg:RowControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectionState}" Value="None">
<Setter Property="Background" Value="#FFD9EDF7"/>
</DataTrigger>
</Style.Triggers>
</Style>
</dxg:TableView.RowStyle>
</dxg:TableView>
</dxg:GridControl.View>
<dxg:GridControl.DetailDescriptor>
<dxg:DataControlDetailDescriptor ItemsSourcePath="SaleItemSub">
<dxg:GridControl AutoGenerateColumns="AddNew">
<dxg:GridControl.View>
<dxg:TableView DetailHeaderContent="SaleItemSub" AutoWidth="True" ShowGroupPanel="False" ShowColumnHeaders="False" RowMinHeight="35" ShowVerticalLines="False" ShowHorizontalLines="False">
<dxg:TableView.RowStyle>
<Style TargetType="{x:Type dxg:RowControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectionState}" Value="None">
<Setter Property="Background" Value="#FFFCF8E3"/>
</DataTrigger>
</Style.Triggers>
</Style>
</dxg:TableView.RowStyle>
</dxg:TableView>
</dxg:GridControl.View>
<dxg:GridColumn FieldName="Item Name" DisplayMemberBinding="{Binding SaleItemMainName}" HorizontalHeaderContentAlignment="Center"/>
</dxg:GridControl>
</dxg:DataControlDetailDescriptor>
</dxg:GridControl.DetailDescriptor>
<dxg:GridColumn FieldName="Item Name" DisplayMemberBinding="{Binding SaleItemMainName}" HorizontalHeaderContentAlignment="Center"/>
<dxg:GridColumn FieldName="Quantity" DisplayMemberBinding="{Binding SaleItemMainQuantity}" HorizontalHeaderContentAlignment="Center"/>
<dxg:GridColumn FieldName="Unit Price" DisplayMemberBinding="{Binding SaleItemMainUnitPrice}" HorizontalHeaderContentAlignment="Center"/>
</dxg:GridControl>
</dxg:DataControlDetailDescriptor>
</dxg:GridControl.DetailDescriptor>
<dxg:GridColumn FieldName="SaleID" DisplayMemberBinding="{Binding SaleID}" Visible="False"/>
<dxg:GridColumn FieldName="Date" DisplayMemberBinding="{Binding SaleDate}" HorizontalHeaderContentAlignment="Center">
<dxg:GridColumn.EditSettings>
<dxe:DateEditSettings Mask="D" MaskUseAsDisplayFormat="True" />
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
<dxg:GridColumn FieldName="Time" DisplayMemberBinding="{Binding SaleDate}" HorizontalHeaderContentAlignment="Center">
<dxg:GridColumn.EditSettings>
<dxe:DateEditSettings Mask="t" MaskUseAsDisplayFormat="True" />
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
<dxg:GridColumn FieldName="Total" DisplayMemberBinding="{Binding SaleTotal}" HorizontalHeaderContentAlignment="Center"/>
<dxg:GridColumn FieldName="Customer" DisplayMemberBinding="{Binding SaleCustomer}" HorizontalHeaderContentAlignment="Center"/>
<dxg:GridColumn FieldName="Employee" DisplayMemberBinding="{Binding SaleEmployee}" HorizontalHeaderContentAlignment="Center"/>
<dxg:GridColumn FieldName="Status" DisplayMemberBinding="{Binding SaleStatus}" Visible="False" />
</dxg:GridControl>
还有我的 C# Class:
public class SaleClass
{
private int saleID;
private DateTime saleDate;
private decimal saleTotal;
private string saleCustomer;
private string saleEmployee;
private string saleStatus;
private ObservableCollection<SaleItemMainClass> saleItemMain;
public SaleClass(int saleID, DateTime saleDate, decimal saleTotal, string saleCustomer, string saleEmployee, string saleStatus)
{
this.saleID = saleID;
this.saleDate = saleDate;
this.saleTotal = saleTotal;
this.saleCustomer = saleCustomer;
this.saleEmployee = saleEmployee;
this.saleStatus = saleStatus;
this.saleItemMain = new ObservableCollection<SaleItemMainClass>();
}
public SaleClass() { }
public int SaleID { get { return saleID; } set { saleID = value; } }
public DateTime SaleDate { get { return saleDate; } set { saleDate = value; } }
public decimal SaleTotal { get { return saleTotal; } set { saleTotal = value; } }
public string SaleCustomer { get { return saleCustomer; } set { saleCustomer = value; } }
public string SaleEmployee { get { return saleEmployee; } set { saleEmployee = value; } }
public string SaleStatus { get { return saleStatus; } set { saleStatus = value; } }
public ObservableCollection<SaleItemMainClass> SaleItemMain { get { return saleItemMain; } }
}
public class SaleItemMainClass
{
private int saleItemMainNumber;
private string saleItemMainName;
private int saleItemMainQuantity;
private decimal saleItemMainUnitPrice;
private decimal saleItemMainTotalPrice;
private int saleItemMainID;
private ObservableCollection<SaleItemSubClass> saleItemSub;
public SaleItemMainClass(int saleItemMainNumber, string saleItemMainName, int saleItemMainQuantity, decimal saleItemMainUnitPrice, decimal saleItemMainTotalPrice, int saleItemMainID)
{
this.saleItemMainNumber = saleItemMainNumber;
this.saleItemMainName = saleItemMainName;
this.saleItemMainQuantity = saleItemMainQuantity;
this.saleItemMainUnitPrice = saleItemMainUnitPrice;
this.saleItemMainTotalPrice = saleItemMainTotalPrice;
this.saleItemMainID = saleItemMainID;
this.saleItemSub = new ObservableCollection<SaleItemSubClass>();
}
public int SaleItemMainNumber { get { return saleItemMainNumber; } set { saleItemMainNumber = value; } }
public string SaleItemMainName { get { return saleItemMainName; } set { saleItemMainName = value; } }
public int SaleItemMainQuantity { get { return saleItemMainQuantity; } set { saleItemMainQuantity = value; } }
public decimal SaleItemMainUnitPrice { get { return saleItemMainUnitPrice; } set { saleItemMainUnitPrice = value; } }
public decimal SaleItemMainTotalPrice { get { return saleItemMainTotalPrice; } set { saleItemMainTotalPrice = value; } }
public int SaleItemMainID { get { return saleItemMainID; } set { saleItemMainID = value; } }
public ObservableCollection<SaleItemSubClass> SaleItemSub { get { return saleItemSub; } }
}
public class SaleItemSubClass
{
private int saleItemSubNumber;
private string saleItemSubName;
private int saleItemSubQuantity;
private decimal saleItemSubUnitPrice;
private decimal saleItemSubTotalPrice;
private int saleItemSubID;
public SaleItemSubClass(int saleItemSubNumber, string saleItemSubName, int saleItemSubQuantity, decimal saleItemSubUnitPrice, decimal saleItemSubTotalPrice, int saleItemSubID)
{
this.saleItemSubNumber = saleItemSubNumber;
this.saleItemSubName = saleItemSubName;
this.saleItemSubQuantity = saleItemSubQuantity;
this.saleItemSubUnitPrice = saleItemSubUnitPrice;
this.saleItemSubTotalPrice = saleItemSubTotalPrice;
this.saleItemSubID = saleItemSubID;
}
public int SaleItemSubNumber { get { return saleItemSubNumber; } set { saleItemSubNumber = value; } }
public string SaleItemSubName { get { return saleItemSubName; } set { saleItemSubName = value; } }
public int SaleItemSubQuantity { get { return saleItemSubQuantity; } set { saleItemSubQuantity = value; } }
public decimal SaleItemSubUnitPrice { get { return saleItemSubUnitPrice; } set { saleItemSubUnitPrice = value; } }
public decimal SaleItemSubTotalPrice { get { return saleItemSubTotalPrice; } set { saleItemSubTotalPrice = value; } }
public int SaleItemSubID { get { return saleItemSubID; } set { saleItemSubID = value; } }
}
列的 FieldName 属性 必须是您要显示的 属性 的路径:ItemName 或 SaleTotal 或 SaleItemMainName 等
您可以像这样用 Header 属性 定义 header:
<dxg:GridControl AutoGenerateColumns="None" EnableSmartColumnsGeneration="False" ItemsSource="{Binding Persons}">
<dxg:GridControl.View>
<dxg:TableView AllowPerPixelScrolling="True" ShowTotalSummary="True" EditorShowMode="MouseUp"/>
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="ForeName" Header="Vorname" />
<dxg:GridColumn FieldName="LastName" Header="Nachname" />
</dxg:GridControl.Columns>
</dxg:GridControl>
通常情况下,如果我手动定义我的列,我会将 AutoGenerateColumns 设置为 None,否则可能会添加一些意外的列。
只有在特殊情况下才需要 DisplayMemberBinding,如果您的单元格应该以特殊方式显示。
我想知道如何在 Master-Detail 网格控件中更改列 header 的名称。我已经使用代码
成功更改了 Master header 的名称<dxg:GridColumn FieldName="SaleID" DisplayMemberBinding="{Binding SaleID}" Visible="False"/>
然而,当我尝试将相同的应用到我的详细信息时:
<dxg:GridColumn FieldName="Item Name" DisplayMemberBinding="{Binding SaleItemMainName}" HorizontalHeaderContentAlignment="Center"/>
它添加了另一列而不是重命名 header 所以我得到了两列具有 SaleItemMainName 的值,它们的 header 被称为:
- 项目名称
- 促销品主要名称
作为参考,这是我的网格控件的 XAML 代码:
<dxg:GridControl Grid.Row="1" x:Name="salePendingGC" AutoGenerateColumns="AddNew" Margin="20" Grid.ColumnSpan="2" Visibility="Hidden" >
<dxg:GridControl.View>
<dxg:TableView x:Name="salePendingTable" DetailHeaderContent="Sales" AutoWidth="True" ShowGroupPanel="False" RowMinHeight="40">
<dxg:TableView.RowStyle>
<Style TargetType="{x:Type dxg:RowControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectionState}" Value="None">
<Setter Property="Background" Value="#FFF2DEDE"/>
</DataTrigger>
</Style.Triggers>
</Style>
</dxg:TableView.RowStyle>
</dxg:TableView>
</dxg:GridControl.View>
<dxg:GridControl.DetailDescriptor>
<dxg:DataControlDetailDescriptor ItemsSourcePath="SaleItemMain">
<dxg:GridControl AutoGenerateColumns="AddNew">
<dxg:GridControl.View>
<dxg:TableView DetailHeaderContent="SaleItemMain" AutoWidth="True" ShowGroupPanel="False" ShowColumnHeaders="True" RowMinHeight="35" ShowVerticalLines="False" ShowHorizontalLines="False">
<dxg:TableView.RowStyle>
<Style TargetType="{x:Type dxg:RowControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectionState}" Value="None">
<Setter Property="Background" Value="#FFD9EDF7"/>
</DataTrigger>
</Style.Triggers>
</Style>
</dxg:TableView.RowStyle>
</dxg:TableView>
</dxg:GridControl.View>
<dxg:GridControl.DetailDescriptor>
<dxg:DataControlDetailDescriptor ItemsSourcePath="SaleItemSub">
<dxg:GridControl AutoGenerateColumns="AddNew">
<dxg:GridControl.View>
<dxg:TableView DetailHeaderContent="SaleItemSub" AutoWidth="True" ShowGroupPanel="False" ShowColumnHeaders="False" RowMinHeight="35" ShowVerticalLines="False" ShowHorizontalLines="False">
<dxg:TableView.RowStyle>
<Style TargetType="{x:Type dxg:RowControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectionState}" Value="None">
<Setter Property="Background" Value="#FFFCF8E3"/>
</DataTrigger>
</Style.Triggers>
</Style>
</dxg:TableView.RowStyle>
</dxg:TableView>
</dxg:GridControl.View>
<dxg:GridColumn FieldName="Item Name" DisplayMemberBinding="{Binding SaleItemMainName}" HorizontalHeaderContentAlignment="Center"/>
</dxg:GridControl>
</dxg:DataControlDetailDescriptor>
</dxg:GridControl.DetailDescriptor>
<dxg:GridColumn FieldName="Item Name" DisplayMemberBinding="{Binding SaleItemMainName}" HorizontalHeaderContentAlignment="Center"/>
<dxg:GridColumn FieldName="Quantity" DisplayMemberBinding="{Binding SaleItemMainQuantity}" HorizontalHeaderContentAlignment="Center"/>
<dxg:GridColumn FieldName="Unit Price" DisplayMemberBinding="{Binding SaleItemMainUnitPrice}" HorizontalHeaderContentAlignment="Center"/>
</dxg:GridControl>
</dxg:DataControlDetailDescriptor>
</dxg:GridControl.DetailDescriptor>
<dxg:GridColumn FieldName="SaleID" DisplayMemberBinding="{Binding SaleID}" Visible="False"/>
<dxg:GridColumn FieldName="Date" DisplayMemberBinding="{Binding SaleDate}" HorizontalHeaderContentAlignment="Center">
<dxg:GridColumn.EditSettings>
<dxe:DateEditSettings Mask="D" MaskUseAsDisplayFormat="True" />
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
<dxg:GridColumn FieldName="Time" DisplayMemberBinding="{Binding SaleDate}" HorizontalHeaderContentAlignment="Center">
<dxg:GridColumn.EditSettings>
<dxe:DateEditSettings Mask="t" MaskUseAsDisplayFormat="True" />
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
<dxg:GridColumn FieldName="Total" DisplayMemberBinding="{Binding SaleTotal}" HorizontalHeaderContentAlignment="Center"/>
<dxg:GridColumn FieldName="Customer" DisplayMemberBinding="{Binding SaleCustomer}" HorizontalHeaderContentAlignment="Center"/>
<dxg:GridColumn FieldName="Employee" DisplayMemberBinding="{Binding SaleEmployee}" HorizontalHeaderContentAlignment="Center"/>
<dxg:GridColumn FieldName="Status" DisplayMemberBinding="{Binding SaleStatus}" Visible="False" />
</dxg:GridControl>
还有我的 C# Class:
public class SaleClass
{
private int saleID;
private DateTime saleDate;
private decimal saleTotal;
private string saleCustomer;
private string saleEmployee;
private string saleStatus;
private ObservableCollection<SaleItemMainClass> saleItemMain;
public SaleClass(int saleID, DateTime saleDate, decimal saleTotal, string saleCustomer, string saleEmployee, string saleStatus)
{
this.saleID = saleID;
this.saleDate = saleDate;
this.saleTotal = saleTotal;
this.saleCustomer = saleCustomer;
this.saleEmployee = saleEmployee;
this.saleStatus = saleStatus;
this.saleItemMain = new ObservableCollection<SaleItemMainClass>();
}
public SaleClass() { }
public int SaleID { get { return saleID; } set { saleID = value; } }
public DateTime SaleDate { get { return saleDate; } set { saleDate = value; } }
public decimal SaleTotal { get { return saleTotal; } set { saleTotal = value; } }
public string SaleCustomer { get { return saleCustomer; } set { saleCustomer = value; } }
public string SaleEmployee { get { return saleEmployee; } set { saleEmployee = value; } }
public string SaleStatus { get { return saleStatus; } set { saleStatus = value; } }
public ObservableCollection<SaleItemMainClass> SaleItemMain { get { return saleItemMain; } }
}
public class SaleItemMainClass
{
private int saleItemMainNumber;
private string saleItemMainName;
private int saleItemMainQuantity;
private decimal saleItemMainUnitPrice;
private decimal saleItemMainTotalPrice;
private int saleItemMainID;
private ObservableCollection<SaleItemSubClass> saleItemSub;
public SaleItemMainClass(int saleItemMainNumber, string saleItemMainName, int saleItemMainQuantity, decimal saleItemMainUnitPrice, decimal saleItemMainTotalPrice, int saleItemMainID)
{
this.saleItemMainNumber = saleItemMainNumber;
this.saleItemMainName = saleItemMainName;
this.saleItemMainQuantity = saleItemMainQuantity;
this.saleItemMainUnitPrice = saleItemMainUnitPrice;
this.saleItemMainTotalPrice = saleItemMainTotalPrice;
this.saleItemMainID = saleItemMainID;
this.saleItemSub = new ObservableCollection<SaleItemSubClass>();
}
public int SaleItemMainNumber { get { return saleItemMainNumber; } set { saleItemMainNumber = value; } }
public string SaleItemMainName { get { return saleItemMainName; } set { saleItemMainName = value; } }
public int SaleItemMainQuantity { get { return saleItemMainQuantity; } set { saleItemMainQuantity = value; } }
public decimal SaleItemMainUnitPrice { get { return saleItemMainUnitPrice; } set { saleItemMainUnitPrice = value; } }
public decimal SaleItemMainTotalPrice { get { return saleItemMainTotalPrice; } set { saleItemMainTotalPrice = value; } }
public int SaleItemMainID { get { return saleItemMainID; } set { saleItemMainID = value; } }
public ObservableCollection<SaleItemSubClass> SaleItemSub { get { return saleItemSub; } }
}
public class SaleItemSubClass
{
private int saleItemSubNumber;
private string saleItemSubName;
private int saleItemSubQuantity;
private decimal saleItemSubUnitPrice;
private decimal saleItemSubTotalPrice;
private int saleItemSubID;
public SaleItemSubClass(int saleItemSubNumber, string saleItemSubName, int saleItemSubQuantity, decimal saleItemSubUnitPrice, decimal saleItemSubTotalPrice, int saleItemSubID)
{
this.saleItemSubNumber = saleItemSubNumber;
this.saleItemSubName = saleItemSubName;
this.saleItemSubQuantity = saleItemSubQuantity;
this.saleItemSubUnitPrice = saleItemSubUnitPrice;
this.saleItemSubTotalPrice = saleItemSubTotalPrice;
this.saleItemSubID = saleItemSubID;
}
public int SaleItemSubNumber { get { return saleItemSubNumber; } set { saleItemSubNumber = value; } }
public string SaleItemSubName { get { return saleItemSubName; } set { saleItemSubName = value; } }
public int SaleItemSubQuantity { get { return saleItemSubQuantity; } set { saleItemSubQuantity = value; } }
public decimal SaleItemSubUnitPrice { get { return saleItemSubUnitPrice; } set { saleItemSubUnitPrice = value; } }
public decimal SaleItemSubTotalPrice { get { return saleItemSubTotalPrice; } set { saleItemSubTotalPrice = value; } }
public int SaleItemSubID { get { return saleItemSubID; } set { saleItemSubID = value; } }
}
列的 FieldName 属性 必须是您要显示的 属性 的路径:ItemName 或 SaleTotal 或 SaleItemMainName 等
您可以像这样用 Header 属性 定义 header:
<dxg:GridControl AutoGenerateColumns="None" EnableSmartColumnsGeneration="False" ItemsSource="{Binding Persons}">
<dxg:GridControl.View>
<dxg:TableView AllowPerPixelScrolling="True" ShowTotalSummary="True" EditorShowMode="MouseUp"/>
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="ForeName" Header="Vorname" />
<dxg:GridColumn FieldName="LastName" Header="Nachname" />
</dxg:GridControl.Columns>
</dxg:GridControl>
通常情况下,如果我手动定义我的列,我会将 AutoGenerateColumns 设置为 None,否则可能会添加一些意外的列。
只有在特殊情况下才需要 DisplayMemberBinding,如果您的单元格应该以特殊方式显示。