无法向可观察集合列表框添加新行
本文关键字:列表 添加 新行 集合 观察 | 更新日期: 2023-09-27 17:56:50
在我的项目中,我有以下页面:
Mainwindow.xaml
窗口1.xaml
用户.cs
App.xaml
我在User.cs
申报了Name
和Age
财产,MainWindow.xaml
我为ListBox
做了一个ObservableCollection
。 MainWindow
有一个Button Add
.当我们点击时 Add button
.然后显示Window1.xaml
窗体。它有两个姓名和年龄TextBoxes
和一个按钮(ADD Name
)。现在,我希望当我们单击Add Name button
时,两个文本框中的详细信息都将附加到Mainwindow.xaml.cs
上定义的ObservableCollection
。
请建议我想要我可以做:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void OnInit(object sender, RoutedEventArgs e)
{
string textA=textBox1.Text;
int textB=Convert.ToInt32(textBox2.Text);
this.DataContext = new User(textA, textB);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
User user = (User)(this.DataContext);
new MainWindow().observableCollection.Add(user);
this.Close();
}
}
现在我运行了这段代码,我无法获取用户对象中的值。
问题是您没有访问MainWindow
CbservableCollection,而是正在创建一个新的MainWindow
。
如果此Window1
是一个对话框,您有几个选项
- 将主窗口作为其所有者传递到 Window1 中
- 使用 Window1 作为对话框,并在关闭时获取更改
我个人认为第二种选择是最好的,但这取决于你如何称呼Window1
示例 2:
主窗口类
public partial class MainWindow : Window
{
private ObservableCollection<User> _myList = new ObservableCollection<User>();
public MainWindow()
{
InitializeComponent();
}
public ObservableCollection<User> MyCollection
{
get { return _myList; }
set { _myList = value; }
}
private void button1_Click_1(object sender, RoutedEventArgs e)
{
var dialog = new Window1();
if (dialog.ShowDialog() == true)
{
MyCollection.Add(dialog.NewUser);
}
}
}
主窗口 xaml
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="233" Width="405" Name="UI">
<Grid DataContext="{Binding ElementName=UI}" >
<ListBox ItemsSource="{Binding MyCollection}" DisplayMemberPath="TextA" Margin="0,0,0,47" />
<Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="0,0,0,12" Name="button1" VerticalAlignment="Bottom" Width="75" Click="button1_Click_1" />
</Grid>
</Window>
窗口1 xaml
<Window x:Class="WpfApplication8.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="119" Width="300" Name="UI">
<StackPanel DataContext="{Binding ElementName=UI}">
<TextBox Text="{Binding NewUser.TextA}" />
<TextBox Text="{Binding NewUser.TextB}" />
<Button Click="button1_Click" HorizontalAlignment="Right" Width="90" Height="30" Content="Add" />
</StackPanel>
</Window>
窗口 1 代码
public partial class Window1 : Window, INotifyPropertyChanged
{
private User _newUser = new User();
public Window1()
{
InitializeComponent();
}
public User NewUser
{
get { return _newUser; }
set { _newUser = value; NotifyPropertyChanged("NewUser"); }
}
private void button1_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
用户类
public class User : INotifyPropertyChanged
{
private string _textA;
private string _textB;
public string TextA
{
get { return _textA; }
set { _textA = value; NotifyPropertyChanged("TextA"); }
}
public string TextB
{
get { return _textB; }
set { _textB = value; NotifyPropertyChanged("TextB"); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}