[英]How to make List<Object> variable appear in DataBinding UI when using WPF in Visual Studio?
I'm in the process of learning WPF and currently exploring data binding. 我正在学习WPF,目前正在探索数据绑定。 I have a DataGrid
control on my form, and in my C# code for the form I have a List<string>
variable. 我在表单上有一个DataGrid
控件,在表单的C#代码中,我有一个List<string>
变量。
I want to be able to use the Properties UI for the DataGrid
in the designer to bind the List<string>
to the DataGrid
. 我希望能够在设计器中使用DataGrid
的Properties UI将List<string>
绑定到DataGrid
。 I cannot figure out what I need to do or where I need to look in the UI to do this. 我无法弄清楚我需要做什么或需要在UI中查看的位置。
This is what I am doing: 这就是我在做什么:
DataGrid
in the UI designer to make it the active control. 在UI设计器中单击我的DataGrid
,使其成为活动控件。 Properties
window. 移至“ Properties
窗口。 ItemsSource
property. 向下滚动到ItemsSource
属性。 And when I get to this point I no longer know what to do. 当我到达这一点时,我不再知道该怎么办。
I do not want to accomplish this by writing/modifying XAML. 我不想通过编写/修改XAML来实现此目的。 I want to know how it works using the UI. 我想知道如何使用UI。
Having never used the designer before, I can't be totally sure (your use case isn't quite clear either). 我以前从未使用过设计师,所以不能完全确定(您的用例也不十分清楚)。
That being said, in my designer you 话虽这么说,在我的设计师你
Note that this is the same as writing in XAML: 请注意,这与使用XAML编写相同:
<DataGrid ItemsSource="{Binding MyItemCollection}"/>
<!-- or --!>
<DataGrid ItemsSource="{Binding Path=MytItemsCollection}"/>
There's a reason no one uses the designer.... 没有人使用设计师是有原因的。
The other options are more "advanced" binding concepts that you don't normally use on ItemsSource properties. 其他选项是通常在ItemsSource属性上不常用的“高级”绑定概念。
Note that DataGrid
is a poor choice for displaying strings. 请注意, DataGrid
是显示字符串的不佳选择。 ListView
or ListBox
are much better choices, as they don't assume your information has multiple pieces (like DataGrid
does). ListView
或ListBox
是更好的选择,因为它们不假定您的信息有多个部分(就像DataGrid
一样)。
I understand not liking XAML, as it really intimidated me at first, but I will quickly say that it is a powerful tool. 我理解不喜欢XAML,因为它起初确实吓倒了我,但是我很快会说它是一个强大的工具。 I am not sure how to do it through the designer, but in C# let's say you name your DataGrid 'myDataGrid' and your List is named 'stringList'. 我不确定如何通过设计器执行此操作,但是在C#中,假设您将DataGrid命名为“ myDataGrid”,而列表则命名为“ stringList”。 It is as simple as the following: 它很简单,如下所示:
myDataGrid.ItemsSource = stringList;
and the data grid is now bound to your string list. 现在,数据网格已绑定到您的字符串列表。
Thanks for asking the question! 感谢您提出问题! The properties window is so underrated. 属性窗口被低估了。 First you must set the DataContext. 首先,您必须设置DataContext。 It's in the common section of the properties window. 它位于属性窗口的公共部分。 Set the data context to whatever view model you need. 将数据上下文设置为所需的任何视图模型。 If you don't have a VM and the List is in the code behind, set the data context to relative source self. 如果您没有VM,并且列表位于后面的代码中,则将数据上下文设置为相对源self。 Next in the Path write the name of your List. 在路径的下一步中,输入列表的名称。 Also, you may want to use ObservableCollection instead of List so your objects are updated in the UI as they change. 另外,您可能希望使用ObservableCollection而不是List,以便在对象更改时在UI中对其进行更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.