locked
transfer data between wpf windows RRS feed

  • Question

  • Hi

    in a window in wpf i have listView that bind to data base and a button under that list view

    when user click on button show new window an a listview with same data that takeed from first window

    or

    how i can transfer data between two listview that are between two windows (in wpf)

    ok

    Sunday, August 22, 2010 7:52 PM

Answers

  • Perhaps you can assign the same instance of data collection object to two different windows' DataContext.

    Example: (ListView data will be updated after changed data in the main window datagrid)

    <Window x:Class="BindingToMultipleWindows.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Name="mainWin"
            Title="MainWindow" Height="350" Width="525">
        <StackPanel>
            <ListView ItemsSource="{Binding}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
                        <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"/>
                    </GridView>
                </ListView.View>
            </ListView>
            <DataGrid ItemsSource="{Binding}"/>
            <Button Content="Launch second window" Click="Button_Click"/>
        </StackPanel>
    </Window>
    using System.Collections.ObjectModel;
    using System.Windows;

    namespace BindingToMultipleWindows
    {
        public partial class MainWindow : Window
        {
            ObservableCollection<Person> people = new ObservableCollection<Person>
            {
                new Person {Name = "Tom", Age = 10},
                new Person {Name = "Dick", Age = 20},
                new Person {Name = "Harry", Age = 30},
            };

            public MainWindow()
            {
                InitializeComponent();
                DataContext = people;
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                new Window1() { DataContext = people }.Show();
            }
        }
        public class Person
        {
            public string Name { set; get; }
            public int Age { set; get; }
        }
    }


    <Window x:Class="BindingToMultipleWindows.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1" Height="300" Width="300">
        <StackPanel>
            <ListView ItemsSource="{Binding}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
                        <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"/>
                    </GridView>
                </ListView.View>
            </ListView>
        </StackPanel>
    </Window>


    William
    • Proposed as answer by KP_SES Monday, August 23, 2010 2:33 AM
    • Marked as answer by Min Zhu Monday, August 30, 2010 1:41 AM
    Sunday, August 22, 2010 8:50 PM