WPF/C# 组合框数据绑定
首先,我想为我的英语远非完美而道歉(它不是我的母语......)。
我的 XAML 代码中存在与数据绑定相关的问题。我有一个组合框,它应该列出我放在自定义画布上的所有图形节点。我的图形节点在列表 graphCanvas.uinodes
中引用,每个节点都有一个名称
。这就是我想在组合框中显示的内容。
所以我尝试了这样的事情:
<ComboBox ItemsSource="{Binding ElementName=graphCanvas, Path=uinodes/name}"
Height="23" HorizontalAlignment="Left" Name="comboBox1"
VerticalAlignment="Top" Width="71" Foreground="Black" />
但即使在画布上绘制节点之后,我的组合框也是空的......
有任何线索吗?
谢谢。
To begin I would like to apologize for my english which is far from perfect (its not my native language...).
I've a problem related to databinding in my XAML code. I've a combox which is supposed to list all graphical nodes that I drop on a custom canvas. My graphical nodes are referenced in a list graphCanvas.uinodes
, and each node has a name
. And that's what I want to show in my combobox.
So I tried something like this:
<ComboBox ItemsSource="{Binding ElementName=graphCanvas, Path=uinodes/name}"
Height="23" HorizontalAlignment="Left" Name="comboBox1"
VerticalAlignment="Top" Width="71" Foreground="Black" />
But even after drawing nodes on my canvas my combox is empty...
Any clue?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
ElementName
的绑定会查找具有该名称的 WPF 元素。我怀疑您是否已对 Canvas 进行子类化并向其添加了 uinodes 属性,这是Path 找到某些内容的唯一方法,即使路径语法是正确的,但事实并非如此。
如果您在运行程序时查看“输出”窗口,您将看到一条错误消息,告诉您绑定不起作用的原因。这是一个开始。
但即便如此,通过这种方法你也不会得到你想要的东西。你可能想要的看起来更像是:
或者甚至
A binding using
ElementName
finds the WPF element with that name. I doubt that you've subclassedCanvas
and added auinodes
property to it, which is the only way thatPath
would find something even if the path syntax were correct, which it's not.If you look in the Output window when you run your program, you'll see an error message that tells you why the binding isn't working. That's a start.
But even then, you won't get what you want with this approach. What you probably want looks more like:
or even
您的绑定(特别是
Path
分配)看起来错误。假设 uinodes 是某种类型的 Enumerable,看起来您正试图绑定到集合的“name”属性,但该属性并不存在。试试这个:顺便说一句,您可以使用输出窗口来查看任何绑定错误。
Your binding (specifically the
Path
assignment) looks wrong. Assuminguinodes
is an Enumerable of some sort it looks as though you are trying to bind to the `name' property of the collection, which does not exist. Try this:As an aside, you can use the output window to see any binding errors.