繁体   English   中英

在User Control中向datagrid添加数据

[英]Adding data to datagrid in User Control

我用户控制了两个窗口,我坚持使用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>

你有两个GridControl实例。

在GridWindow中,它是<local:GridControl> </local:GridControl> ,它显示没有数据,

和MainWindow有GridControl ngridControl = new GridControl(); 包含数据但不在任何地方显示的实例。

可以做些什么?

最好的解决方案是将应用程序架构更改为MVVM并为您的窗口创建视图模型。 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);
    }
}

GridWindow.xaml<GridControl>元素一个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);
    }
}

暂无
暂无

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

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