如何在XAML和C#中将字符串列表显示为DataGrid(表) [英] How to display a List of Strings as a DataGrid (table) in XAML and C#
问题描述
我长期以来一直潜心研究SO,直到最近才决定开设一个帐户。我已经花了很多时间试图解决我一直遇到的这个问题,但是我一直在这里。
I've been a long time lurker on SO and have only recently decided to set up an account. I've been spending quite a lot of hours trying to solve this problem I've been having without asking but I here it is.
我要完成的工作:
我有一个字符串列表,例如:Mango,Banana ,Melon(我们称它为Fruit),我想将其显示为XAML(WPF)中的表格,它在左侧显示为行值,而右侧则是组合框,允许用户选择一个值。我目前只能解决此问题。我决定使用DataGrid来显示列表,后面的代码将进行数据绑定。
I have a list of strings, e.g: Mango, Banana, Melon (let's call it fruits) and I want to display it as a table in XAML (WPF), where it appears as row values on the left side, and the right side will be combo boxes that will allow users to pick a value. I'm currently stuck on the displaying part of this problem. I decided to use a DataGrid to display the list, and the code behind will do the data bindings.
这里是XAML的一部分:
Here's the XAML part of it:
<DataGrid x:Name="FruitDataGrid" Height="265" VerticalAlignment="Center" Margin="-7,8,-2,-6" HorizontalAlignment="Left" Width="1188" AutoGenerateColumns="False">
<!-- If AutoGenerateColumns was true only the length is displayed.-->
<DataGrid.Columns>
<DataGridTextColumn x:Name="fruitsDisplay" Header="Fruits" MinWidth="450" IsReadOnly="True" />
<DataGridComboBoxColumn Header="Number of Boxes" MinWidth ="200" CanUserResize="True" Width="*"></DataGridComboBoxColumn>
</DataGrid.Columns>
到目前为止的代码已经
private void populateFruitList()
{
FruitDataGrid.DataContext = fruitDataTable;
//Binds a datatable instance to the datagrid, does not display anything.
}
我一直在尝试:
将字符串列表转换成一个Observable集合或Enumerable,然后执行FruitDataGrid.itemsource = fruitsObservable;
Turn the List of Strings into an Observable Collection, or Enumerable, and do FruitDataGrid.itemsource = fruitsObservable;
上述方法有效,但是它可以只会显示每个字符串值的长度(如果autogeneratecolumns为true),这不是我想要的。如果autogeneratecolumns为false,那么我可以看到显示的行,但看不到字符串值。
The above method works, except that it will only display the length of each string value (if autogeneratecolumns is true), which is not what I want. If autogeneratecolumns was false then I can see the rows being shown, but not the string values.
我也尝试使用DataView或DataGridView,但是我未能在XAML中定义它,VS2012表示没有这种东西。
I also tried using DataView or DataGridView but I failed to define it in the XAML, VS2012 says that there isn't such a thing.
我一直在尝试进行数据绑定以及 MSDN文章说,但是VS2012从未设法正确绑定到列表。
I've been trying to do data binding as well as the MSDN Article says, but VS2012 never manages to bind to the list properly.
I然后尝试将我的列表更改为数据表,并[使用此处指定的datagridview,但XAML再次告诉我它不是有效的类] [2]。我也知道datagridview可能会对性能产生影响,但是目前我只想显示一个表。
I then attempted to change my list into a datatable and [use a datagridview as specified here but again the XAML tells me it is not a valid class][2]. I am also aware of the performance impact datagridview might have but at this point I just want to display a table really.
我还看到有些教程使用DataGrid.datasource方法,但是我想要的DataGrid中没有该方法,我认为这是一个不同的类吗? (我尝试使用的DataGrid是System.Windows.Controls.DataGrid,另一个是Windows窗体[如此处所示] [3])
I've also seen that some tutorials use a DataGrid.datasource method but that isn't in the DataGrid I want, I think it's a different class? (the DataGrid I am attempting to use is System.Windows.Controls.DataGrid, and the other one is in Windows Forms [As shown here][3])
谢谢再次抽出时间对此进行研究。
Thanks again for taking the time to look into this.
编辑
尝试以这种方式绑定到XAML中的列表:
Trying to bind to the list in XAML this way:
<DataGrid x:Name="FruitDataGrid" Height="265" VerticalAlignment="Center" Margin="-7,8,-2,-6" HorizontalAlignment="Left" Width="1188" AutoGenerateColumns="False" ItemsSource="fruitDataList">
我收到XAML错误: IEnumerable的类型转换器不支持从字符串转换我认为是因为我做错了。该表现在显示了很多空行。
I get the error in XAML "The type converter for IEnumerable does not support converting from a string" which I think is because I'm doing it wrong. The table now shows a lot of empty rows though.
尝试ItemSource = {Binding fruitDataList}(其中fruitDataList是列表)会产生一个空白表,并在VS中出现BindingExpression路径错误。
Trying ItemSource="{Binding fruitDataList}" (where fruitDataList is a List) yields a blank table, and an error in VS for BindingExpression path error.
推荐答案
考虑到绑定已正确设置为视图模型。.
Considering your binding is set to your viewmodel correctly.. do this
ItemsSource="{Binding fruitDataList}"
然后
<DataGrid.Columns>
<DataGridTextColumn
x:Name="fruitsDisplay"
Binding="{Binding Path=FRUITS_PROPERTY_NAME_IN_COLLECTION}"
Header="Fruits"
MinWidth="450"
IsReadOnly="True" />
<DataGridComboBoxColumn
ItemsSource="{Binding TO_List_of_Numbers}"
Header="Number of Boxes"
MinWidth ="200"
CanUserResize="True" Width="*"></DataGridComboBoxColumn>
这篇关于如何在XAML和C#中将字符串列表显示为DataGrid(表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!