是否可以将 Listxaml 中的静态资源?
如果我想在代码隐藏中绑定类似组合框的东西,我完全没有问题。类似这样的事情:
List<string> strings = new List<string>();
AddStringsFromDataSourceToList(strings);
comboBox1.ItemSource = strings;
据我所知,在 XAML 中没有快速而肮脏的方法来做到这一点。尽管 wpf 因其超级简单的数据绑定而受到好评,但这种简单的事情似乎在 C# 中更容易做到。有没有比创建 DependencyProperty 包装器并将它们添加为资源更简单的方法,而无需智能感知或所有进入 ObservableCollections 的帮助?我知道这并非不可能,但如果这样一个简单的任务看起来如此深入,我一定会错过一些东西......
编辑:为了澄清,添加动态列表是这里的问题,而不是静态数组。正如许多人指出的那样,手动添加项目非常容易。
If I want to bind something like a combobox in the code-behind I have no problem at all. Something like :
List<string> strings = new List<string>();
AddStringsFromDataSourceToList(strings);
comboBox1.ItemSource = strings;
As far as I can tell, there is no quick and dirty way to do this in XAML. For all of the praise wpf is receiving for its super simple databinding, something this simple seems far easier to just do in C#. Is there an easier way to do this than creating DependencyProperty wrappers and adding them as resources without much help from intellisense or all that goes into ObservableCollections? I understand that its not impossible, but I must be missing something if such a simple task seems so in depth...
EDIT: To clarify, adding dynamic Lists is the issue here, not static arrays. It is super easy to add items manually, as many have pointed out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否正在寻找这样的东西:
如果您只想定义列表的某些项目,请查看 Henk Holterman 的解决方案。顺便说一句,您也可以将数组声明为资源和其他类型。
更新
您似乎更改了您的问题。但我现在不明白你的愿望是什么。如果您想绑定代码隐藏中的集合,请创建一个返回此集合的公共属性,将 DataContext 设置为公开此属性的对象,并在 XAML 中定义一个 Binding:
希望我正确理解您的问题。 。
Are you looking for something like this:
If you only want to define some items for a list, look at the solution from Henk Holterman. By the way, you can declare the array also as a resource and for other types.
Update
It seems that you have changed your question. However I don't understand now what your desire is. If you want to bind a collection you have in your code-behind, then make a public property that returns this collection, set the DataContext to the object that exposes this property and define a Binding in XAML:
Hope I understood your question right...
当然有:
根据您的目标,还有其他几乎同样简单的语法 - 使用资源或内联项目源,或分组数据..甚至 xml 数据。不要因为你尝试的第一件事并不容易而沮丧地举手 - 在我看来,wpf 值得学习。
WPF 之所以受到赞扬,是因为它比 Windows 窗体更容易将视觉效果与行为分离,并且因为它使创建漂亮的视觉效果变得更加容易,而不是因为它使简单的示例变得更加容易。然而,在这种情况下 - 做这个简单的例子更容易。
根据您的编辑您想从哪里提取它们?您不必以任何方式创建依赖属性或可观察集合。一个简单的列表属性就可以了(在这种情况下,我更喜欢在 xaml 中使用 collectionviewsource )。另外,请不要忘记,如果您不喜欢 XAML,则不需要使用所有 XAML。但是,如果您针对 WPF 进行设计,而不是不管它,您会发现很多任务(比如这个)很容易。
Sure there is:
There are other syntaxes depending on your goal that are nearly as simple - using resources or inline itemssources, or grouped data.. even xml data. Don't throw up your hands in frustration because the first thing you tried wasn't easy - wpf is worth the learning curve, in my opinion.
WPF gets praise because it makes separating the visuals from the behavior much easier than windows forms, and because it makes creating nice visual effects much much easier, not because it makes it easier to do trivial examples. However, in this case - it is easier to do the trivial example.
With your edit Where do you want to pull them from? You don't have to create dependency properties or observable collections by any means. A simple list property will do (I prefer to use a collectionviewsource in the xaml in that case). Also, don't forget that you don't need to use all XAML if you hate it. But if you design for WPF instead of in spite of it you'll find a lot of tasks (like this one) easy.
您可以这样做:
并且还存在用于在资源部分中声明(和隐式创建)数据的语法。
也许您可以指出一个更完整的场景,以及要求和约束?
You can do:
And there also exists syntax for declaring (and implicitly creating) data in for example a Resource section.
Maybe you can point out a more full scenario, with the requirements and constraints ?