【发布时间】: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