简体   繁体   English

无法将数据网格绑定到我可观察的类对象集合

[英]Unable to bind Data Grid to my observable collection of class object

I am writing a GUI where I have multiple tabs.我正在编写一个 GUI,其中有多个选项卡。 each tab has a header and content.每个选项卡都有标题和内容。 Tab is bonded to observable collections of object Item. Tab 绑定到对象 Item 的可观察集合。 The content of the tab is a DataGrid which i want to bind to another object LogGUIData (which is a class member variable of Item).选项卡的内容是一个 DataGrid,我想将其绑定到另一个对象 LogGUIData(这是 Item 的类成员变量)。 Data is populated by socket read.数据由套接字读取填充。 I tired to find lot of answers but couldn't find any.我厌倦了找到很多答案,但找不到任何答案。 I know I am missing somewhere something as I am new to C# and WPF programming.我知道我在某处遗漏了一些东西,因为我是 C# 和 WPF 编程的新手。 My code is below.我的代码如下。 Thanks in advance提前致谢


<TabControl.ContentTemplate>
                        <DataTemplate>
                            <DataGrid Name="dgLogdata"  Margin="10" VerticalScrollBarVisibility="Visible" ItemsSource="{Binding LogDataOC, Mode=TwoWay}"
                                      AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" >                              
                                <DataGrid.Columns>
                                    <DataGridTextColumn Header="Header" Binding="{Binding strHeaderData}" FontFamily="Arial" />
                                </DataGrid.Columns>
                            </DataGrid>
                        </DataTemplate>
                    </TabControl.ContentTemplate>

c# code C#代码

public class Item : INotifyPropertyChanged
        {

            public string Header { get; set; }

            public static int _count = -1;
            public int Count
            {
                get { return _count; }
                set { _count = value; }
            }


            public Item()
            {
                LogDataOC = new ObservableCollection<GUILogData>();
                _count++;//increase the count of tab. This will represent the index of the tab
            }

            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string propertyName)
            {
                var handler = PropertyChanged;
                handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }

            private ObservableCollection<GUILogData> _logDataOC { get; set; }

            public ObservableCollection<GUILogData> LogDataOC
            {
                get { return _logDataOC; }
                set
                {
                    _logDataOC = value;
                    OnPropertyChanged("LogDataOC");
                }
            }
        }

I was binding to the private member instead of the property.我绑定到私有成员而不是属性。 Modified Xaml code:修改后的 Xaml 代码:

<TabControl.ContentTemplate>
                        <DataTemplate>
                            <DataGrid Name="dgLogdata"  Margin="10" VerticalScrollBarVisibility="Visible" ItemsSource="{Binding LogDataOC, Mode=TwoWay}"
                                      AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" >                              
                                <DataGrid.Columns>
                                    *<DataGridTextColumn Header="Header" Binding="{Binding HeaderData}" FontFamily="Arial" />*                                  
                                </DataGrid.Columns>
                            </DataGrid>
                        </DataTemplate>
                    </TabControl.ContentTemplate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将可观察的集合模型属性绑定到网格中的下拉列 - Bind observable collection model property to dropdown column in grid 绑定可观察的集合 - Bind Observable collection 将类的字典绑定到数据网格 - Bind dictionary of a class to data grid WPF绑定到XAML声明的类实例中的Observable Collection - WPF Bind to Observable Collection within XAML declared class instance WPF双向数据绑定到可观察集合中的自定义数据类型 - WPF two way data bind to custom data type in observable collection 如何将WPF数据网格内的依赖项对象绑定到可观察的集合 - How to bind a dependency object inside a wpf datagrid to a observable collection 无法将其绑定到我的网格到Telerik OpenAccess数据源 - Unable to bind it to my Grid to a Telerik OpenAccess DataSource 如何将Observable Collection上的空数据解析为数据网格绑定? - How to resolve null data on Observable Collection to data grid binding? 点击按钮后没有数据从数据网格流向 Observable Collection - No data flows from data grid to Observable Collection after hitting a button 对数据网格中的列进行自定义评估,并将Observable Collection绑定到DataGrid的列 - custom evaluation of columns in data grid and Binding of Observable Collection to a Column of DataGrid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM