I made two windows one with user control and i stuck with datagrid. I want to add data in MainWindow and show it in UserControl(Window2) but it's not working, when i change usercontrol to simple window it's fine and works. How can I rewrite It? Im newbie and just learning c# and wpf :)
MainWindow.xaml
public partial class MainWindow : Window
{
GridControl ngridControl = new GridControl();
GridWindow ngridWindow = new GridWindow();
public MainWindow()
{
InitializeComponent();
ngridWindow.Show();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Information item = new Information();
item.id = name_box.ToString();
item.name = number_box.ToString();
ngridControl.informationList.Add(item);
}
}
GridControl.xaml
public partial class GridControl : UserControl
{
public ObservableCollection<Information> informationList = new ObservableCollection<Information>();
public GridControl()
{
InitializeComponent();
dataGrid.ItemsSource = informationList;
}
}
}
Information.class
public class Information
{
public string name { get; set; }
public string id { get; set; }
}
GridWindow.xaml
<Window x:Class="WpfApp5.GridWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp5"
mc:Ignorable="d"
Title="GridWindow" Height="450" Width="800">
<Grid>
<local:GridControl> </local:GridControl>
</Grid>
you have two instances of GridControl.
in GridWindow it is <local:GridControl> </local:GridControl>
which is displayed without data,
and MainWindow has GridControl ngridControl = new GridControl();
instance which contains data but not displayed anywhere.
what can be done?
the best solution is to change app architecture to MVVM and create view models for your windows. ObservableCollection<Information> informationList
will become a property of GridWindowViewModel. GridControl will have a DependencyProperty for binding informationList
, instead of common property. MainWindowViewModel will be able to create GridWindowviewModel, use it as a DataContext for GriwWindow and add items to informationList
.
the local fix is to work with one GridControl instance in both windows. GridWindow should allow to add item to GridControl.
<Window x:Class="WpfApp5.GridWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp5"
mc:Ignorable="d"
Title="GridWindow" Height="450" Width="800">
<Grid>
<local:GridControl> </local:GridControl>
</Grid>
</Window>
public partial class GridWindow : Window
{
public GridWindow()
{
InitializeComponent();
}
public void AddInformation(Information item)
{
ngridControl.AddInformation(item);
}
}
public partial class MainWindow : Window
{
GridWindow ngridWindow = new GridWindow();
public MainWindow()
{
InitializeComponent();
ngridWindow.Show();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Information item = new Information();
item.id = name_box.ToString();
item.name = number_box.ToString();
ngridWindow.AddInformation(item);
}
}
Give the <GridControl>
element in GridWindow.xaml
an x:Name
:
<local:GridControl x:Name="theControl"> </local:GridControl>
...and add the item to this one's informationList
:
public partial class MainWindow : Window
{
GridWindow ngridWindow = new GridWindow();
public MainWindow()
{
InitializeComponent();
ngridWindow.Show();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Information item = new Information();
item.id = name_box.ToString();
item.name = number_box.ToString();
ngridWindow.theControl.informationList.Add(item);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.