[英]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.