Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Bind ItemsSource with ObservableCollection in WPF
    primarykey
    data
    text
    <p>In my WPF Application - I add new item to <code>ObservableCollection</code> via <code>Button Click Event Handler</code>. Now i want to show this added item immediately as it adds to <code>ObservableCollection</code> via <code>Binding</code> to <code>ItemsControl</code> I wrote code but it is not working. Can anyone solve my problem. Here code is:</p> <p><strong>.XAML File</strong></p> <pre><code> &lt;dxlc:ScrollBox VerticalAlignment="Top"&gt; &lt;ItemsControl x:Name="lstItemsClassM" ItemsSource="{Binding Path=topp, Mode=TwoWay}"&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;Button Content="{Binding Name}" Tag="{Binding PKId}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/dxlc:ScrollBox&gt; </code></pre> <p><strong>.CS FILE</strong></p> <pre><code> public ObservableCollection&lt;ClassMM&gt; topp { get; set; } int dv , startindex, lastindex; public MainWindow() { InitializeComponent(); topp = new ObservableCollection&lt;ClassMM&gt;(); 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)); } } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload