简体   繁体   English

如何制作清单 <Object> 在Visual Studio中使用WPF时,变量是否出现在DataBinding UI中?

[英]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: 这就是我在做什么:

  1. Click my DataGrid in the UI designer to make it the active control. 在UI设计器中单击我的DataGrid ,使其成为活动控件。
  2. Move to the Properties window. 移至“ Properties窗口。
  3. Scroll down to the ItemsSource property. 向下滚动到ItemsSource属性。
  4. Click on the field and the UI with Source, Path, Converter and Options pops up. 单击该字段,然后弹出带有“源”,“路径”,“转换器”和“选项”的UI。

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 话虽这么说,在我的设计师你

  1. Set the "Binding Type" to "Data Context" 将“绑定类型”设置为“数据上下文”
  2. Select the "Custom" text box (needed for me because it doesn't see my DataContext) 选择“自定义”文本框(对我来说是必须的,因为它看不到我的DataContext)
  3. Type the name of your property in the "Path" field (you can only bind to Properties) 在“路径”字段中输入属性的名称(您只能绑定到属性)
  4. Hit OK. 点击确定。

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). ListViewListBox是更好的选择,因为它们不假定您的信息有多个部分(就像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.

相关问题 如何在 visual studio 上使用 WPF 框架制作扩展表单? - How to make an expanding form using WPF framework on visual studio? 使用用户控件时的WPF数据绑定 - WPF DataBinding when using usercontrols 使用WPF数据绑定设计响应式UI - Designing responsive UI using WPF databinding 如何在启动Visual Studio时显示对话框。 - VS扩展 - How to make a dialog appear when Visual Studio is started. - VS Extension 在带有C#的Visual Studio中,如何在键入时自动显示(),例如.Focus()? - In Visual Studio with C#, how do I make the () appear automatically when I type, such as .Focus()? WPF数据绑定未更新UI - WPF Databinding Not Updating UI 如何在Visual Studio中正确显示MVC项目? - How do I make a MVC project appear properly in Visual Studio? 如何使我的自定义类型出现在Visual Studio设置编辑器中? - How to make my custom type to appear in Visual Studio Settings Editor? 如何使用文件WPF在CheckEd中进行数据绑定 - how to make databinding in CheckEd it with file WPF 在Visual Studio C#中使用自定义DLL时,如何显示自定义的intellisense注释? - How do you get custom intellisense comments to appear when using a custom DLL in Visual Studio C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM