【问题标题】:DataGrid - Binding double array to columnDataGrid - 将双精度数组绑定到列
【发布时间】:2015-12-31 15:11:16
【问题描述】:

在我的 DataGrid 中,我有三列,行数是动态的。 DataGrid 的值是双精度数组。如何在不创建新类的情况下将每个数组绑定到他的列(我在每个数组上都有 propertychangedevent)

<DataGrid Name="dataGrid" HorizontalContentAlignment="Center" VerticalContentAlignment="Stretch"
                              AutoGenerateColumns="False" Style="{DynamicResource DataGridStyle1}" 
                              CellEditEnding="dataGrid_Kennlinie_CellEditEnding" BeginningEdit="dataGrid_Kennlinie_BeginningEdit"
                              MaxWidth="500">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="nue" Binding="{Binding nue}" Width="*">
                                <DataGridTextColumn.Foreground>
                                    <SolidColorBrush Color="Black"/>
                                </DataGridTextColumn.Foreground>
                            </DataGridTextColumn>

                            <DataGridTextColumn Header="mue" Binding="{Binding mue}" Width="*"/>
                            <DataGridTextColumn Header="tpc[Nm]" Binding="{Binding MPc}" Width="*" />
....end

nue、mue 和 MPc 是另一个类之间的数组。当我这样做的时候

dataGrid.ItemSource = class.nue;

在这个类中,我创建了包含我需要的变量的类,这些是:

private double[] _nue;
    public double[] nue
    {
        get { return _nue; }
        set
        {
            if (_nue == value) return;
            _nue = value;
            OnPropertyChanged("_nue");
        }
    }
    private double[] _mue;
    public double[] mue
    {
        get { return _mue; }
        set
        {
            if (_mue == value) return;
            _mue = value;
            OnPropertyChanged("_mue");
        }
    }
    private double[] _MPc;
    public double[] MPc
    {
        get { return _MPc; }
        set
        {
            if (_MPc == value) return;
            _MPc = value;
            OnPropertyChanged("_MPc");
        }
    }

它为我设置了正确的行数,但没有值。

有什么想法吗? 谢谢,新年快乐

【问题讨论】:

  • 你应该把你用作数据源的类的代码。
  • 我不需要整个类,只需要三个变量。

标签: c# arrays wpf binding datagrid


【解决方案1】:

在您的示例中,您只是绑定到一个数组 (nue)。 因此行数是正确的,但没有显示数据。因为nue.nue, nue.mue等不存在。 (您应该会在输出窗口中看到有关缺少绑定的错误。)

最简单的解决方案是重构您的类,并绑定该类的 List、Array 或 ObervableCollection。

如果您从某处获得双精度数组作为输入:无法将此数据映射到其他结构中,DataGrid 默认可以处理。

// simplified
class Container {
  public double Nue {get; set;}
  public double Mue {get; set;}
  public double MPc {get; set;}
}

ObservableCollection<Container> containers = ...
dataGrid.ItemSource = containers;

// This will now update the grid, just like other operations on the ObservableCollection
containers.Add(new Container {
   Nue = 13.1,
   Mue = 2.23,
   MPc = 0.01
});

DataGrid xaml 可以保持原样,绑定现在应该可以工作了。

【讨论】:

  • 但是我在原件上没有我的 propertychanged 事件,我只是用我需要的值编写新变量
  • Container 可以实现 INotifyPropertyChanged 并且您可以在其属性上引发 OnPropertyChanged。
  • 但是还有双精度值。所以当我引发一个事件时,它只会更改双精度值,而不是整个数组
  • 这是正确的。但由于它是一个ObservableCollection,它会根据添加和删除值进行更新。
  • 是的绑定有效,但这就是我所说的,只是“复制”我需要的值,而不是引发事件来更改数组
猜你喜欢
  • 1970-01-01
  • 2013-04-13
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多