简体   繁体   English

在User Control中向datagrid添加数据

[英]Adding data to datagrid in User Control

I made two windows one with user control and i stuck with datagrid. 我用户控制了两个窗口,我坚持使用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. 我想在MainWindow中添加数据并在UserControl(Window2)中显示它,但它不起作用,当我将usercontrol更改为简单窗口时,它很好并且有效。 How can I rewrite It? 我怎么能重写呢? Im newbie and just learning c# and wpf :) 我新手,只是学习c#和wpf :)

MainWindow.xaml 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 GridControl.xaml

public partial class GridControl : UserControl
{
    public ObservableCollection<Information> informationList = new ObservableCollection<Information>();

    public GridControl()
    {
        InitializeComponent();
        dataGrid.ItemsSource = informationList;
    }
}

} }

Information.class Information.class

public class Information
{
    public string name { get; set; }
    public string id { get; set; }
}

GridWindow.xaml 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. 你有两个GridControl实例。

in GridWindow it is <local:GridControl> </local:GridControl> which is displayed without data, 在GridWindow中,它是<local:GridControl> </local:GridControl> ,它显示没有数据,

and MainWindow has GridControl ngridControl = new GridControl(); 和MainWindow有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. 最好的解决方案是将应用程序架构更改为MVVM并为您的窗口创建视图模型。 ObservableCollection<Information> informationList will become a property of GridWindowViewModel. ObservableCollection<Information> informationList将成为GridWindowViewModel的属性。 GridControl will have a DependencyProperty for binding informationList , instead of common property. GridControl将具有用于绑定informationList的DependencyProperty,而不是公共属性。 MainWindowViewModel will be able to create GridWindowviewModel, use it as a DataContext for GriwWindow and add items to informationList . MainWindowViewModel将能够创建GridWindowviewModel,将其用作GriwWindow的DataContext并将项添加到informationList

the local fix is to work with one GridControl instance in both windows. 本地修复是在两个窗口中使用一个GridControl实例。 GridWindow should allow to add item to 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);
    }
}

Give the <GridControl> element in GridWindow.xaml an x:Name : GridWindow.xaml<GridControl>元素一个x:Name

<local:GridControl x:Name="theControl"> </local:GridControl>

...and add the item to this one's informationList : ...并将项目添加到此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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM