在数据网格中显示子属性

时间:2014-06-05 19:20:28

标签: c# wpf binding datagrid

简短:如何将DataGridColumn绑定到对象的属性,但显示前者的子属性?

长:我有一个简单的类

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,另一列应该显示MeasuredValue的{​​{1}}。但我无法绑定ExperimentResult,因为那样 不会抛出ExperimentResult.MeasuredValue事件。

所以问题是:如何显示样本的PropertyChanged

1 个答案:

答案 0 :(得分:0)

没有什么可以阻止你绑定到不会在它的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>

供参考,这是我的代码背后:     public partial class MainWindow:Window     {         public MainWindow()         {             的InitializeComponent();             this.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; }
}