无法在observablecollection列表框中添加新行?

时间:2013-01-31 02:07:02

标签: c# wpf listbox observablecollection datacontext

在我的项目中,我有以下几页:

  

Mainwindow.xaml

     

Window1.xaml

     

User.cs

     

的App.xaml

我在NameAge上声明了User.csMainWindow.xaml属性我为ObservableCollection做了ListBoxMainWindow有一个Button Add。当我们点击Add button时。然后显示Window1.xaml表单。其中有两个TextBoxes名称和年龄以及一个按钮(ADD Name)。现在我想点击Add Name button,然后两个文本框中的详细信息都会附加到ObservableCollection上定义的Mainwindow.xaml.cs

请建议我可以这样做:

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

现在我运行了这段代码,我无法获取用户对象中的值。

1 个答案:

答案 0 :(得分:2)

问题是您没有访问MainWindow CbservableCollection,而是创建了新的MainWindow

如果此Window1是对话框,则您有几个选项

  1. 将MainWindow作为其所有者
  2. 传递给Window1
  3. 将Window1用作对话框并在关闭时获取更改
  4. 我个人认为第二种选择是最好的,但这取决于你如何调用Window1

    例2:

    MainWindow Class

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

    MainWindow 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>
    

    Window1 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>
    

    Window1代码

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