簡體   English   中英

無法將數據網格綁定到我可觀察的類對象集合

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

我正在編寫一個 GUI,其中有多個選項卡。 每個選項卡都有標題和內容。 Tab 綁定到對象 Item 的可觀察集合。 選項卡的內容是一個 DataGrid,我想將其綁定到另一個對象 LogGUIData(這是 Item 的類成員變量)。 數據由套接字讀取填充。 我厭倦了找到很多答案,但找不到任何答案。 我知道我在某處遺漏了一些東西,因為我是 C# 和 WPF 編程的新手。 我的代碼如下。 提前致謝


<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#代碼

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");
                }
            }
        }

我綁定到私有成員而不是屬性。 修改后的 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM