In my WPF Application - I add new item to ObservableCollection
via Button Click Event Handler
. Now i want to show this added item immediately as it adds to ObservableCollection
via Binding
to ItemsControl
I wrote code but it is not working. Can anyone solve my problem. Here code is:
.XAML File
<dxlc:ScrollBox VerticalAlignment="Top">
<ItemsControl x:Name="lstItemsClassM" ItemsSource="{Binding Path=topp, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Button Content="{Binding Name}" Tag="{Binding PKId}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</dxlc:ScrollBox>
.CS FILE
public ObservableCollection<ClassMM> topp { get; set; }
int dv , startindex, lastindex;
public MainWindow()
{
InitializeComponent();
topp = new ObservableCollection<ClassMM>();
startindex=dv=1;
topp.Add(new ClassMM() { PKId=dv, Name = "Test 1" });
dv=2;
topp.Add(new ClassMM() { PKId = dv, Name = "Test 2" });
dv = 3;
topp.Add(new ClassMM() { PKId = dv, Name = "Test 3" });
dv = 4;
topp.Add(new ClassMM() { PKId = dv, Name = "Test 4" });
lastindex=dv = 5;
topp.Add(new ClassMM() { PKId = dv, Name = "Test 5" });
}
private void Button_Click(object sender, RoutedEventArgs e)
{
lastindex = dv = dv++;
topp.Add(new ClassMM() { PKId = dv, Name = musavebutton.Content.ToString() });
foreach (var jk in topp.ToList())
{
MessageBox.Show(jk.Name);
}
}
public class ClassMM : INotifyPropertyChanged
{
public string _name;
public int _pkid;
public int PKId
{
get { return _pkid; }
set
{
if (value != _pkid)
{
_pkid = value;
NotifyPropertyChanged();
}
}
}
public string Name
{
get { return _name; }
set
{
if (value != _name)
{
_name = value;
NotifyPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
WPF provides the ObservableCollection<T> class, which is a built-in implementation of a data collection that implements the INotifyCollectionChanged interface.
One more important difference is you can access ObservableCollection only from thread on which it was created where as list can be accessed fromany thread. Save this answer. Show activity on this post. I see no problem with that, other than a very marginal performance overhead.
ItemsSource can be data bound to any sequence that implements the IEnumerable interface, although the type of collection used does determine the way in which the control is updated when items are added to or removed. When ItemsSource is set, the Items property cannot be used to control the displayed values.
Data binding in Windows Presentation Foundation (WPF) provides a simple and consistent way for apps to present and interact with data. Elements can be bound to data from different kinds of data sources in the form of . NET objects and XML.
Keep your XAML as original and modify you cs as follows :
public ObservableCollection<ClassMM> topp { get; set; }
private int dv, startindex, lastindex;
public MainWindow()
{
InitializeComponent();
DataContext = this;
topp = new ObservableCollection<ClassMM>();
startindex = dv = 1;
topp.Add(new ClassMM() {PKId = dv, Name = "Test 1"});
dv = 2;
topp.Add(new ClassMM() {PKId = dv, Name = "Test 2"});
dv = 3;
topp.Add(new ClassMM() {PKId = dv, Name = "Test 3"});
dv = 4;
topp.Add(new ClassMM() {PKId = dv, Name = "Test 4"});
lastindex = dv = 5;
topp.Add(new ClassMM() {PKId = dv, Name = "Test 5"});
}
private void Button_Click(object sender, RoutedEventArgs e)
{
lastindex = dv = dv++;
topp.Add(new ClassMM() { PKId = dv, Name = musavebutton.Content.ToString() });
foreach (var jk in topp.ToList())
{
MessageBox.Show(jk.Name);
}
}
public class ClassMM : INotifyPropertyChanged
{
public string _name;
public int _pkid;
public int PKId
{
get { return _pkid; }
set
{
if (value != _pkid)
{
_pkid = value;
NotifyPropertyChanged("PKId");
}
}
}
public string Name
{
get { return _name; }
set
{
if (value != _name)
{
_name = value;
NotifyPropertyChanged("Name");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With