C# 如何在WPF中将ItemsSource与ObservableCollection绑定
在我的WPF应用程序中-我通过C# 如何在WPF中将ItemsSource与ObservableCollection绑定,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,在我的WPF应用程序中-我通过按钮单击事件处理程序将新项添加到可观测集合。现在我想立即显示这个添加的项,因为它通过绑定到ItemsControl添加到ObservableCollection,但它不起作用。谁能解决我的问题。代码如下: .XAML文件 <dxlc:ScrollBox VerticalAlignment="Top"> <ItemsControl x:Name="lstItemsClassM" ItemsSource="{Binding Pa
按钮单击事件处理程序将新项添加到可观测集合
。现在我想立即显示这个添加的项,因为它通过绑定到ItemsControl
添加到ObservableCollection
,但它不起作用。谁能解决我的问题。代码如下:
.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文件
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));
}
}
}
publicobservableCollection top{get;set;}
intdv,startindex,lastindex;
公共主窗口()
{
初始化组件();
topp=新的ObservableCollection();
startindex=dv=1;
Add(new ClassMM(){PKId=dv,Name=“Test 1”});
dv=2;
Add(new ClassMM(){PKId=dv,Name=“Test 2”});
dv=3;
Add(new ClassMM(){PKId=dv,Name=“Test 3”});
dv=4;
Add(new ClassMM(){PKId=dv,Name=“Test 4”});
lastindex=dv=5;
Add(new ClassMM(){PKId=dv,Name=“Test 5”});
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
lastindex=dv=dv++;
Add(new ClassMM(){PKId=dv,Name=musavebutton.Content.ToString()});
foreach(topp.ToList()中的变量jk)
{
MessageBox.Show(jk.Name);
}
}
公共类ClassMM:INotifyPropertyChanged
{
公共字符串\u名称;
公共国际公钥基础设施;
公共密钥基础设施
{
获取{return}pkid;}
设置
{
如果(值!=\u pkid)
{
_pkid=值;
NotifyPropertyChanged();
}
}
}
公共字符串名
{
获取{return\u name;}
设置
{
如果(值!=\u名称)
{
_名称=值;
NotifyPropertyChanged();
}
}
}
公共事件属性更改事件处理程序属性更改;
受保护的void NotifyPropertyChanged(字符串propertyName=”“)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}保持您的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));
}
}
}
publicobservableCollection top{get;set;}
私有int dv、startindex、lastindex;
公共主窗口()
{
初始化组件();
DataContext=this;
topp=新的ObservableCollection();
startindex=dv=1;
Add(new ClassMM(){PKId=dv,Name=“Test 1”});
dv=2;
Add(new ClassMM(){PKId=dv,Name=“Test 2”});
dv=3;
Add(new ClassMM(){PKId=dv,Name=“Test 3”});
dv=4;
Add(new ClassMM(){PKId=dv,Name=“Test 4”});
lastindex=dv=5;
Add(new ClassMM(){PKId=dv,Name=“Test 5”});
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
lastindex=dv=dv++;
Add(new ClassMM(){PKId=dv,Name=musavebutton.Content.ToString()});
foreach(topp.ToList()中的变量jk)
{
MessageBox.Show(jk.Name);
}
}
公共类ClassMM:INotifyPropertyChanged
{
公共字符串\u名称;
公共国际公钥基础设施;
公共密钥基础设施
{
获取{return}pkid;}
设置
{
如果(值!=\u pkid)
{
_pkid=值;
NotifyPropertyChanged(“PKId”);
}
}
}
公共字符串名
{
获取{return\u name;}
设置
{
如果(值!=\u名称)
{
_名称=值;
NotifyPropertyChanged(“名称”);
}
}
}
公共事件属性更改事件处理程序属性更改;
受保护的void NotifyPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
这是不正确的:ItemsSource=“{Binding-topp,Mode=TwoWay}”
TwoWay
用于获取和设置绑定属性本身,在本例中是topp
,而不是列表的内容<代码>可观察列表
处理项目添加/删除即时通知。在这种情况下,您不希望items控件与topp
的值混淆,因此正确的绑定只是{binding topp}
否。请仔细查看!topp是可观察的集合
列表。我想将其绑定到ItemsControl
public observeablecollection的itemsSource
,topp{get;set;}是observeablecollection类型的属性。当使用NotifyPropertyChanged方法进行更改时,它确实需要填充更改。正如我在回答中提到的,你必须结合上下文。请尝试并让我们知道。为什么要将topp
的数据类型写为integer
?很抱歉,我的错。我复制了您创建的第二个属性,并对其进行了处理=),正如前面所说的,您需要将DataConxtext设置为包含属性topp的类。我已经有一段时间没有编写WPF了,但是您不应该记住XAML中的路径吗,也就是说,我猜您的主窗口需要实现iNotifyPropertyChanged。但是HichemCSharp的答案中设置的NotifyPropertyChanged是