我用一个用户控件制作了两个窗口,我坚持使用datagrid。 我想在MainWindow中添加数据并在UserControl(Window2)中显示它,但它不起作用,当我将usercontrol更改为简单窗口时,它很好并且有效。我怎么能重写呢?我是新手,只是学习c#和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>
答案 0 :(得分:2)
你有两个GridControl实例。
GridWindow中的是<local:GridControl> </local:GridControl>
,没有数据显示
和MainWindow有GridControl ngridControl = new GridControl();
个实例,其中包含数据但未在任何地方显示。
可以做些什么?
最佳解决方案是将应用程序架构更改为 MVVM ,并为您的Windows创建视图模型。 ObservableCollection<Information> informationList
将成为GridWindowViewModel的属性。 GridControl将具有用于绑定informationList
的DependencyProperty,而不是公共属性。 MainWindowViewModel将能够创建GridWindowviewModel,将其用作GriwWindow的DataContext并将项目添加到informationList
。
本地修复是在两个窗口中使用一个GridControl实例。 GridWindow应该允许将项添加到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);
}
}
答案 1 :(得分:1)
在<GridControl>
GridWindow.xaml
中提供x:Name
元素:
<local:GridControl x:Name="theControl"> </local:GridControl>
...并将该项添加到此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);
}
}