WPF/C# 组合框数据绑定

发布于 2024-11-07 07:42:53 字数 508 浏览 0 评论 0原文

首先,我想为我的英语远非完美而道歉(它不是我的母语......)。

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩于岁月 2024-11-14 07:42:53

使用 ElementName 的绑定会查找具有该名称的 WPF 元素。我怀疑您是否已对 Canvas 进行子类化并向其添加了 uinodes 属性,这是 Path 找到某些内容的唯一方法,即使路径语法是正确的,但事实并非如此。

如果您在运行程序时查看“输出”窗口,您将看到一条错误消息,告诉您绑定不起作用的原因。这是一个开始。

但即便如此,通过这种方法你也不会得到你想要的东西。你可能想要的看起来更像是:

<ComboBox ItemsSource="{Binding ElementName=graphCanvas, Path=uinodes}" 
          DisplayMemberPath="name"/>

或者甚至

<ComboBox ItemsSource="{Binding ElementName=graphCanvas, Path=uinodes}">
   <ComboBox.ItemTemplate>
      <TextBlock Text="{Binding name}"/>
   </ComboBox.ItemTemplate>
</ComboBox>

A binding using ElementName finds the WPF element with that name. I doubt that you've subclassed Canvas and added a uinodes property to it, which is the only way that Path 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:

<ComboBox ItemsSource="{Binding ElementName=graphCanvas, Path=uinodes}" 
          DisplayMemberPath="name"/>

or even

<ComboBox ItemsSource="{Binding ElementName=graphCanvas, Path=uinodes}">
   <ComboBox.ItemTemplate>
      <TextBlock Text="{Binding name}"/>
   </ComboBox.ItemTemplate>
</ComboBox>
总攻大人 2024-11-14 07:42:53

您的绑定(特别是 Path 分配)看起来错误。假设 uinodes 是某种类型的 Enumerable,看起来您正试图绑定到集合的“name”属性,但该属性并不存在。试试这个:

ItemsSource="{Binding ElementName=graphCanvas, Path=uinodes}" DisplayMemberPath="name"

顺便说一句,您可以使用输出窗口来查看任何绑定错误。

Your binding (specifically the Path assignment) looks wrong. Assuming uinodes 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:

ItemsSource="{Binding ElementName=graphCanvas, Path=uinodes}" DisplayMemberPath="name"

As an aside, you can use the output window to see any binding errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文