【问题标题】:Loading a DataGridView Binded to a Dictionary in Separate Window XAML WPF C#在单独的窗口 XAML WPF C# 中加载绑定到字典的 DataGridView
【发布时间】:2021-01-16 21:34:46
【问题描述】:

我目前正在尝试加载一个单独的窗口,当用户单击地图上的某个位置时会出现该窗口。我希望窗口显示保存在 C# 字典中的业务对象数据。

由于某种原因,我无法让窗口显示任何内容,它只是保持空白。

我在自己的代码中定义所有列/行并填充数据表,而不是在设计器中。

这是我的一些线索代码:

XAML: 业务数据窗口.xaml

   <Window x:Class="BusinessLocator.BusinessDataTable"

    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:DigoohBusinessLocator"

    mc:Ignorable="d"

    Title="BusinessDataTable" Height="450" Width="800">

<Grid>

    <DataGrid x:Name="BusinessDataGrid" ItemsSource="{Binding Source = BusinessDataGridView}" AutoGenerateColumns="True" HorizontalAlignment="Left" Height="412" VerticalAlignment="Top" Width="785">

    </DataGrid>

</Grid>

这里是 XAML 背后的代码(请原谅一些注释行): BusinessDataWindow.xaml.cs

/// <summary>

/// Interaction logic for BusinessDataTable.xaml

/// </summary>

public partial class BusinessDataWindow : Window

{

    //System.Windows.Controls.DataGrid BusinessDataGrid;

    //DataGridView BusinessDataGridView;

    DataTable BusinessDataTable;

    Dictionary<string, Business> BusinessLocationDictionary;

    public BusinessDataWindow(Dictionary<string, Business> BusinessLocationDictionary)

    {

        //this.BusinessDataGrid = null;

        this.BusinessLocationDictionary = BusinessLocationDictionary;



        this.initializeDataGridView();

        //this.InitializeComponent();

        //this.Loaded += new RoutedEventHandler(DataGrid_Loaded);

    }



    public DataGridView BusinessDataGridView

    {

        get;

        set;

    }



    private void initializeDataGridView()

    {

        this.BusinessDataGridView = new DataGridView();

        this.BusinessDataTable = new DataTable();

        int businessCount = this.BusinessLocationDictionary.Values.Count;



        string nameColumn = "Name";

        string placeIdColumn = "Place ID";

        string addressColumn = "Address";

        string phoneNumberColumn = "Phone Number";

        string OwnerColumn = "Owner";



        // Add the columns to the DataGridView.

        this.BusinessDataTable.Columns.Add(new DataColumn(nameColumn, typeof(string)));

        this.BusinessDataTable.Columns.Add(new DataColumn(placeIdColumn, typeof(string)));

        this.BusinessDataTable.Columns.Add(new DataColumn(addressColumn, typeof(string)));

        this.BusinessDataTable.Columns.Add(new DataColumn(phoneNumberColumn, typeof(string)));

        this.BusinessDataTable.Columns.Add(new DataColumn(OwnerColumn, typeof(string)));

       

        List<Business> BusinessList = this.BusinessLocationDictionary.Values.ToList<Business>();



        // Add the rows to the DataGridView.

        for (int rowIndex = 0; rowIndex < businessCount; rowIndex++)

        {

            this.BusinessDataTable.Rows.Add(BusinessList.ElementAt(rowIndex));

            this.populateDataGridCells();

        }

    }



    private void populateDataGridCells()

    {

        int businessCount = this.BusinessLocationDictionary.Values.Count;

        List<Business> BusinessList = this.BusinessLocationDictionary.Values.ToList<Business>();



        // Populate the rows to the DataGridView.

        for (int rowIndex = 0; rowIndex < businessCount; rowIndex++)

        {

            DataGridViewRow row = this.BusinessDataGridView.Rows[rowIndex];



            row.Cells[rowIndex].Value = BusinessList.ElementAt(rowIndex);

        }



        this.BusinessDataGridView.DataSource = this.BusinessDataTable;

    }

}

【问题讨论】:

  • 希望你没有评论 InitializeComponent() :D
  • 我是否必须自己实现该方法才能显示某些内容?
  • 也在行:DataGridViewRow row = this.BusinessDataGridView.Rows[rowIndex];我收到 System.ArgumentOutOfRangeException。索引超出范围。
  • 当您添加 > 窗口时,存根在 ctor 中有初始化组件。那条线会创建你的用户界面。如果你想要任何视图,在你的构造函数中拥有它是相当重要的。
  • 您正在使用 rowindex 来索引 x 和 y。行和列。这似乎不太可能是正确的。我不遵循你想要做的事情。您有一个似乎已断开连接的数据表和字典。我不明白为什么你有两个。要绑定数据表,您应该将其默认视图绑定到数据网格的项目源。 wpf 中的数据网格。

标签: c# wpf dictionary xaml user-interface


【解决方案1】:

您在构造函数中注释掉了 this.InitializeComponent();。如果不调用此方法,将不会生成任何控件。

所以它应该是这样的:

public BusinessDataWindow(Dictionary<string, Business> BusinessLocationDictionary)
{
            //this.BusinessDataGrid = null;
            this.BusinessLocationDictionary = BusinessLocationDictionary;
            this.initializeDataGridView();
            this.InitializeComponent();
            //this.Loaded += new RoutedEventHandler(DataGrid_Loaded);
}

至于您在 DataGridViewRow row = this.BusinessDataGridView.Rows[rowIndex]; 行中的错误是因为您的 BusinessDataGridView 没有行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2021-08-06
    相关资源
    最近更新 更多