用于填充列表或数组的依赖属性
我有一个自定义控件。它能够与其他几个控件一起执行某些操作。我希望它有一个元素 NotifyControl
,我可以在其中绑定一些其他控件,例如 NotifyControl="{Binding ElementName=controlA}"
。这很好,但我想写下 n
个控件。因此,可能是元素值中的列表或多次标记该元素。喜欢
<MyControl NotifyControl="{Binding ElementName=a}" NotifiyControl="{Binding ElementName=b}" />
或
<MyControl NotifyControl="{Binding ElementName=a}, {Binding ElementName=b}" />
哪一种是可能的以及如何做?我对数组类型没有运气,也许我上面的符号是错误的。
编辑:
我现在有
<MyControl>
<MyControl.NotifyControls>
<NotifyControlWrapper View="{Binding ElementName=details}" Test="entry one" />
<NotifyControlWrapper View="{Binding ElementName=gauge}" Test="e2" />
</MyControl.NotifyControls>
</MyControl>
<OtherControl x:Name="details" />
NotifyControls
是一个 DependencyProperty 并填充了两个条目,因此这部分工作正常。 NotifyControlWrapper
的源只是一个从 DependencyObject
派生的类,具有两个依赖属性 View
(类型 INotifyControl
)和测试
(类型String
)。
正如我所说,我的列表有两个条目,其中有两个 NotifyControlWrapper
。但是,虽然 Test
包含给定的字符串,但 View
为 null
。为什么会这样或者如何调试?
I have a custom control. This has the ability to do something with several other Controls. I would like it to have an element NotifyControl
where I can bind some other controls like NotifyControl="{Binding ElementName=controlA}"
. This is fine but I would like to write down n
controls. So maybe a list in the element value or noting the element multiple times. Like
<MyControl NotifyControl="{Binding ElementName=a}" NotifiyControl="{Binding ElementName=b}" />
or
<MyControl NotifyControl="{Binding ElementName=a}, {Binding ElementName=b}" />
Which one is possible and how do to it? I got no luck with an array type, maybe my notation like above is wrong.
EDIT:
I now have
<MyControl>
<MyControl.NotifyControls>
<NotifyControlWrapper View="{Binding ElementName=details}" Test="entry one" />
<NotifyControlWrapper View="{Binding ElementName=gauge}" Test="e2" />
</MyControl.NotifyControls>
</MyControl>
<OtherControl x:Name="details" />
NotifyControls
is a DependencyProperty and filled with two entries, so this part works fine. The source of NotifyControlWrapper
is just a class derived from DependencyObject
with the two dependency properties View
(type INotifyControl
) and Test
(type String
).
As I sayed my list gets two entries with two NotifyControlWrapper
. But while Test
contains the given String, View
is null
. Why is that or how to debug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题中的任何一个都不可能。您不能两次添加相同的属性,因此#1 不起作用。您无法添加两个绑定,因此#2 不起作用。我将添加一个属性 NotifyControls 作为列表类型。 NotifyControl 仍然可以作为单独的项目使用,或者添加到 NotifyControls 中的控件列表中。您可以在 Xaml 中添加项目:
ControlWrapper 将只有一个成员属性 Control,以便您可以指定绑定。
Neither one in your question is possible. You can't add the same property twice so #1 won't work. You can't add two bindings so #2 won't work. I would add a property NotifyControls as a List type. NotifyControl could still be available as a separate item or to add to the list of controls in NotifyControls. You can add items in Xaml:
ControlWrapper would just have a single member property, Control, so that you can specify the binding.
如果 N 是固定的,则可以使用 MultiBinding(带有转换器):
如果 N 发生变化,则可以选择添加 ObservableCollection<> 。到您添加/删除控件的类,然后绑定到它(同样,使用转换器)
If N is fixed, you can use a MultiBinding (with a converter):
If N changes, an option would be to add an ObservableCollection<> to your class which you add/remove the controls to, and then bind to it (again, with a converter)