在数据网格中显示子属性

本文关键字:显示 属性 网格 数据 数据网 | 更新日期: 2023-09-27 18:06:51

Short:如何将DataGridColumn绑定到对象的属性,但显示前一个的子属性?

Long:我有一个简单的Class

public class Measurement
{
    public double xPosition { get; set; }
    public double yPosition { get; set; }
    public double MeasuredValue { get; set; }
}

由另一个类使用:

public class Sample : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    private string name;
    public string Name
    {
        get { return name; }
        set 
        {
            name = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }
    private Measurement experimentResult;
    public Measurement ExperimentResult
    {
        get
        {
            return experimentResult;
        }
        set
        {
            experimentResult = value;
            PropertyChanged(this, new PropertyChangedEventArgs("ExperimentResult"));
        }
}

我想在DataGrid中显示Sample的列表。一列是Name,另一列是ExperimentResult中的MeasuredValue。但是我不能绑定到ExperimentResult.MeasuredValue,因为不抛出PropertyChanged事件。

所以问题是:如何显示样品的MeasuredValue ?

在数据网格中显示子属性

没有什么可以阻止你绑定到一个不会在setter中引发PropertyChanged事件的属性-但是当属性发生时,你的UI不会更新。至于绑定到子属性,可以通过关闭DataGrid中的自动列生成,然后显式定义所需的列来实现。在此过程中,您可以在Binding语句中引用子属性:

    <DataGrid ItemsSource="{Binding Samples}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}"/>
            <DataGridTextColumn Binding="{Binding ExperimentResult.MeasuredValue}"/>
        </DataGrid.Columns>
    </DataGrid>
作为参考,这是我的代码后面:公共部分类MainWindow: Window{公共主窗口(){InitializeComponent ();这一点。DataContext = new ViewModel();
}}
public class ViewModel
{
    private ObservableCollection<Sample> _samples = new ObservableCollection<Sample>()
    {
        new Sample()
        {
            Name = "Sample1",
            ExperimentResult = new Measurement()
            { MeasuredValue = 100.00, xPosition = 1, yPosition = 0}
        },
        new Sample()
        {
            Name = "Sample2",
            ExperimentResult = new Measurement()
            {
                MeasuredValue = 50.00, xPosition = 2, yPosition = 3}
        }
    };
    public ObservableCollection<Sample> Samples
    {
        get
        {
            return _samples;
        }
    }
}
public class Sample : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }
    private Measurement experimentResult;
    public Measurement ExperimentResult
    {
        get
        {
            return experimentResult;
        }
        set
        {
            experimentResult = value;
            PropertyChanged(this, new PropertyChangedEventArgs("ExperimentResult"));
        }
    }
}
public class Measurement
{
    public double xPosition { get; set; }
    public double yPosition { get; set; }
    public double MeasuredValue { get; set; }
}