DevExpress WPF Unbound Column with Expression Editor

本文关键字:Expression Editor with Column WPF Unbound DevExpress | 更新日期: 2023-09-27 18:21:27

我正试图将一个未绑定的列添加到我的GridControl中,同时启用它上的表达式编辑器,这样当我打开它并编写有效的表达式时,列值应该会相应地更改,但它不起作用。我有一个GridControl,它有3列:第1列、第2列和第3列。第3列是未绑定的列。表达式编辑器会正确打开,并允许我编写表达式,但当我单击"确定"时,值永远不会更改,无论我编写什么。

我使用的是DevExpress 13.1,不可能升级。这是我的代码:

XAML:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
    x:Class="ExpressionEditor.MainWindow"
    Title="Expression Editor" Height="350" Width="525">
    <dxg:GridControl x:Name="myGridControl">
        <dxg:GridControl.Columns>
            <dxg:GridColumn FieldName="Column1" />
            <dxg:GridColumn FieldName="Column2" />
            <dxg:GridColumn FieldName="Column3" 
                            UnboundType="Decimal" 
                            ReadOnly="True" />
        </dxg:GridControl.Columns>
        <dxg:GridControl.View>
            <dxg:TableView x:Name="myTableView" 
                           NavigationStyle="Cell" />
        </dxg:GridControl.View>
    </dxg:GridControl>
</Window>

代码背后:

using System.Collections.ObjectModel;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
    #region Properties
    /// <summary>
    /// Gets or sets the data table2.
    /// </summary>
    /// <value>
    /// The data table2.
    /// </value>
    public ObservableCollection<CustomRow> DataTable { get; set; }
    #endregion
    #region Constructors
    /// <summary>
    /// Initializes a new instance of the <see cref="MainWindow"/> class.
    /// </summary>
    public MainWindow()
    {
        this.InitializeComponent();
        this.DataTable = new ObservableCollection<CustomRow>();
        this.myGridControl.ItemsSource = this.DataTable;
        this.Init();
    }
    #endregion
    #region Methods
    private void Init()
    {
        // Add Data
        this.DataTable.Add(new CustomRow { Column1 = 100, Column2 = 200 });
        this.DataTable.Add(new CustomRow { Column1 = 300, Column2 = 400 });
        // Add Unbound Column
        var column = this.myGridControl.Columns["Column3"];
        column.AllowUnboundExpressionEditor = true;
    }
    #endregion
}

CustomRow类:

/// <summary>
/// Custom Row
/// </summary>
public class CustomRow
{
    #region Properties
    /// <summary>
    /// Gets or sets the column1.
    /// </summary>
    /// <value>
    /// The column1.
    /// </value>
    public double Column1 { get; set; }
    /// <summary>
    /// Gets or sets the column2.
    /// </summary>
    /// <value>
    /// The column2.
    /// </value>
    public double Column2 { get; set; }
    /// <summary>
    /// Gets or sets the column3.
    /// </summary>
    /// <value>
    /// The column3.
    /// </value>
    public double Column3 { get; set; }
    #endregion
}

DevExpress WPF Unbound Column with Expression Editor

好的,我自己找到了答案。问题是,如果将"UNBOUND列"添加到项目源中,则"UNBOUNCD列"的概念将失去意义(UNBOUND表示数据未绑定到任何内容)。因此,这里的解决方案是从CustomRow类中删除"Column3"属性,使XAML上定义的"Column3"成为TRUE未绑定列。