C# 如何在 WPF 中将 ItemsSource 与 ObservableCollection 绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19211721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Bind ItemsSource with ObservableCollection in WPF
提问by user2835256
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:
在我的 WPF 应用程序中 - 我将新项目添加到ObservableCollection
via Button Click Event Handler
. 现在,我想立即显示此添加的项目,因为它增加了 ObservableCollection
通过Binding
给ItemsControl
我写的代码,但它无法正常工作。谁能解决我的问题。这里的代码是:
.XAML File
.XAML 文件
<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
.CS文件
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));
}
}
}
}
}
采纳答案by HichemSeeSharp
Keep your XAML as original and modify you cs as follows :
保持您的 XAML 为原始并修改您的 cs 如下:
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));
}
}
}
回答by Anton Tykhyy
This is incorrect: ItemsSource="{Binding topp, Mode=TwoWay}"
. TwoWay
pertains to getting and setting the bound property itself, in this case topp
, rather than the contents of the list. ObservableList
handles item add/remove notifications out of the box. In this case you don't want the items control to mess with the value of topp
, so the correct binding is just {Binding topp}
.
这是不正确的:ItemsSource="{Binding topp, Mode=TwoWay}"
。TwoWay
与获取和设置绑定属性本身有关,在本例中为topp
,而不是列表的内容。ObservableList
处理开箱即用的项目添加/删除通知。在这种情况下,您不希望 items 控件弄乱 的值topp
,因此正确的绑定只是{Binding topp}
.