1

In my project I have the following pages:

Mainwindow.xaml

Window1.xaml

User.cs

App.xaml

I declared the Name and Age property in User.cs and on MainWindow.xaml I made a ObservableCollection for a ListBox. MainWindow has a Button Add. When we click on the Add button. Then the Window1.xaml form is displayed. Which has two TextBoxes of name and age and a button (ADD Name). Now I want when we click on the Add Name button then the details in the both text boxes are attached to the ObservableCollection that is defined on the Mainwindow.xaml.cs.

Please suggest me want can I do:

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

now I ran this code I am unable to get the values in the user object.

4

1 に答える 1

2

The problem is you are not accessing the MainWindow CbservableCollection, you are creating a new MainWindow.

If this Window1 is a dialog you have a few options

  1. Pass the MainWindow into Window1 as its owner
  2. Use the Window1 as a dialog and fetch the changes when it closes

personally I think the 2nd option is the best, but it depends how you are calling Window1

Example2:

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 code

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

User Class

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));
        }
    }
}
于 2013-01-31T02:46:49.177 に答える