【问题标题】:display a child property in a datagrid在数据网格中显示子属性
【发布时间】:2014-06-05 19:20:28
【问题描述】:

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

Long:我有一个简单的类

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,另一列应显示ExperimentResultMeasuredValue。但我不能绑定到ExperimentResult.MeasuredValue 因为那 不会抛出 PropertyChanged 事件。

那么问题来了:如何显示样本的MeasuredValue

【问题讨论】:

    标签: c# wpf binding datagrid


    【解决方案1】:

    没有什么可以阻止您绑定到不会在其设置器中引发 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 : 窗口 { 公共主窗口() { 初始化组件(); 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; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多