簡體   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