'Connecting an ObservableCollection to Listview for a Windows Application UI (Visual Studio C#)
I'm working on a project where an application displays data by pulling it from a file and displaying a new data value every second. (edit: consolidated code at the end).
While displaying changing data in textblocks work fine, what I'm trying to do is not only display it momentarily but have a display where the data keeps adding to a list and displaying and which can also be saved later (i.e I'm trying to use ListView and ObservableCollection). There's a few components that I'm struggling with:
Working with ListView: I initially did this but the "get" keeps returning null.
public ListView Data2
{
get { return Data2_; }
set
{
Data2_= value;
NotifyPropertyChanged("Data2");
}
}
I was able to create a variable with an increasing list but have trouble connecting it to the UI. I'm not getting any values to show up though. Any help to go about this would be appreciated! Sorry I'm just learning application building so this is all new to me.
Edit: what I have now:
DataDisplay.xaml file:
<ListView x:Name="Data2" Height="100">
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
</Style>
</ListView.Resources>
<ListView.DataContext>
<local:GetData/>
</ListView.DataContext>
<ListView.View>
<GridView>
<GridViewColumn Header="Data 2" Width="250" DisplayMemberBinding="{Binding DATA2_}" />
</GridView>
</ListView.View>
</ListView>
DataDisplay.xaml.cs file:
public partial class DataDisplay : Window { private ObservableCollection data_2;
public DataDisplay ()
{
InitializeComponent();
data_2 = new ObservableCollection<uint>();
}
private async void AddData(object sender, EventArgs e)
{
cnt++;
data_2.Add(data2_;);
Data.data2 = data2_;
}
}
3rd .cs file:
public class GetData: INotifyPropertyChanged
{
private ObservableCollection<uint>Data2;
public ObservableCollection<uint> DATA2_
{
get { return Data2; }
set
{
Data2 = value;
NotifyPropertyChanged("DATA2_");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|