gpt4 book ai didi

c# - 如何在 WPF 中将 ItemsSource 与 ObservableCollection 绑定(bind)

转载 作者:太空狗 更新时间:2023-10-30 01:05:19 25 4
gpt4 key购买 nike

在我的 WPF 应用程序中 - 我通过 Button Click Event Handler 添加新项目到 ObservableCollection。现在我想立即显示这个添加的项目,因为它通过 Binding 添加到 ObservableCollectionItemsControl 我写了代码,但它不工作。谁能解决我的问题。这里的代码是:

.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));
}
}
}

最佳答案

保持您的 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));
}
}
}

关于c# - 如何在 WPF 中将 ItemsSource 与 ObservableCollection 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19211721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com