datagrid绑定不适用于一个数据网格,而是适用于另一个数据网格

时间:2016-01-27 11:02:43

标签: c# .net wpf xaml datagrid

我有一个数据网格(图片在下面)。

您将在行详细信息中看到其他3个数据网格。绑定适用于其中两个,但不是一个令人讨厌的。

不工作的数据网格是a)没有显示列Sedol的值,它应该是& b)不允许我编辑或点击进入单元格。它绑定的对象不为null,因为正在工作的数据网格绑定到同一个对象。我不明白为什么这个datagrid绑定不起作用?

Class HoldingEQ

请注意我已经删除了一些代码。该类实现了INotifyPropertyChanged接口,所有属性都具有

public class HoldingLogEQ : INotifyPropertyChanged
    {            
        public DateTime DateEffective
        {
            get
            {
                return _dateEffective;
            }
            set
            {
                _dateEffective = value;
                OnPropertyChanged("DateEffective");
            }
        }
        public SecruityId Security
        {
            get
            {
                return _security;
            }
            set
            {
                _security = value;
                OnPropertyChanged("Security");
            }
        }
        public double Ratio
        {
            get
            {
                return _ratio;
            }
            set
            {
                _ratio = value;
                OnPropertyChanged("Ratio");                    
            }
        }            
        public List<FundUI> Funds
        {
            get
            {
                return _funds;
            }
            set
            {
                _funds = value;
                OnPropertyChanged("Funds");
            }
        }
        public HoldingLogEQ NewHldLog
        {
            get
            {
                return _newHldLog;
            }
            set
            {
                _newHldLog = value;
                OnPropertyChanged("NewHldLog");
            }
        }

        private DateTime _dateEffective;
        private SecruityId _security = new SecruityId();            
        private double _ratio;
        private List<FundUI> _funds;
        private HoldingLogEQ _newHldLog;

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }

Class SecurityId

public class SecurityId : INotifyPropertyChanged
    {            
        public string ISIN
        {
            get
            {
                return _isin;
            }
            set
            {
                _isin = value;
                OnPropertyChanged("ISIN");
            }
        }
        public string Sedol
        {
            get
            {
                return _sedol;
            }
            set
            {
                _sedol = value;
                OnPropertyChanged("Sedol");
            }
        }
        public string BbergTicker
        {
            get
            {
                return _bbergTicker;
            }
            set
            {
                _bbergTicker = value;
                OnPropertyChanged("BbergTicker");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private string _isin;
        private string _sedol;
        private string _bbergTicker;
    }

无效的数据网格 - 图片右上角的数据网格

<DataGrid Grid.Row="0"
                              ItemsSource="{Binding SelectedItem.NewHldLog,  RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, UpdateSourceTrigger=PropertyChanged}"                                                                  
                              RowStyle="{StaticResource DG_Row}"  
                              ColumnHeaderStyle="{StaticResource DG_ColumnHeader}" 
                              RowHeaderStyle="{StaticResource DG_RowHeaderNested}"
                              CellStyle="{StaticResource DG_Cell}" 
                              Background="Silver"
                              HorizontalGridLinesBrush="LightGray"
                              VerticalGridLinesBrush="LightGray"
                              CanUserAddRows="False"
                              CanUserDeleteRows="False"
                              Margin="50,5,5,0"
                              AutoGenerateColumns="False">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="ISIN" Binding="{Binding Security.ISIN, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" MinWidth="75"/>
                                    <DataGridTextColumn Header="Sedol" Binding="{Binding Security.Sedol, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" MinWidth="75"/>
                                    <DataGridTextColumn Header="Ticker" Binding="{Binding Security.Ticker, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" MinWidth="75"/>
                                    <DataGridTextColumn Header="Name" Binding="{Binding Security.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False" MinWidth="200"/>                                            
                                </DataGrid.Columns>
                            </DataGrid>

正在运行的Datagrid

<DataGrid Grid.Row="1" Grid.Column="0"  Grid.ColumnSpan="2"
                              ItemsSource="{Binding SelectedItem.NewHldLog.Funds,  RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"                                                                  
                              RowStyle="{StaticResource DG_Row}"  
                              ColumnHeaderStyle="{StaticResource DG_ColumnHeader}" 
                              RowHeaderStyle="{StaticResource DG_RowHeaderNested}"
                              CellStyle="{StaticResource DG_Cell}" 
                              Background="Silver"
                              HorizontalGridLinesBrush="LightGray"
                              VerticalGridLinesBrush="LightGray"
                              CanUserAddRows="False"
                              CanUserDeleteRows="False"
                              Margin="50,5,5,20"
                              AutoGenerateColumns="False">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="Fund Code" Binding="{Binding Code}" IsReadOnly="True" MinWidth="75"/>
                                    <DataGridTextColumn Header="Fund Code SS" Binding="{Binding CodeSS}" IsReadOnly="True" MinWidth="75"/>
                                    <DataGridTextColumn Header="Nominal" Binding="{Binding ChangeInNominal}" IsReadOnly="False"/>
                                </DataGrid.Columns>
                            </DataGrid>

图片

Example of datagrid

1 个答案:

答案 0 :(得分:2)

Datagrids希望与某种形式的集合绑定。在正在工作的网格中,您可以看到它绑定到List,而另一个网格绑定到单个对象。

尝试将其更改为一个集合,或者如果它只是一个项目,那么可能会考虑另一个控件来显示它?