用于填充列表或数组的依赖属性

发布于 2024-11-26 10:07:30 字数 1308 浏览 1 评论 0原文

我有一个自定义控件。它能够与其他几个控件一起执行某些操作。我希望它有一个元素 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 包含给定的字符串,但 Viewnull。为什么会这样或者如何调试?

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

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

发布评论

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

评论(2

2024-12-03 10:07:30

你的问题中的任何一个都不可能。您不能两次添加相同的属性,因此#1 不起作用。您无法添加两个绑定,因此#2 不起作用。我将添加一个属性 NotifyControls 作为列表类型。 NotifyControl 仍然可以作为单独的项目使用,或者添加到 NotifyControls 中的控件列表中。您可以在 Xaml 中添加项目:

<MyControl.NotifyControls>
    <ControlWrapper Control="{Binding ElementName=a}"/>
    <ControlWrapper Control="{Binding ElementName=b}"/>
</MyControl.NotifyControls>

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:

<MyControl.NotifyControls>
    <ControlWrapper Control="{Binding ElementName=a}"/>
    <ControlWrapper Control="{Binding ElementName=b}"/>
</MyControl.NotifyControls>

ControlWrapper would just have a single member property, Control, so that you can specify the binding.

迷爱 2024-12-03 10:07:30

如果 N 是固定的,则可以使用 MultiBinding(带有转换器):

<MyControl>
    <MyControl.NotifyControl>
        <MultiBinding Converter="...">
            <Binding ElementName="controlA" />
            <Binding ElementName="controlB" />
            <Binding ElementName="controlC" />
            <Binding ElementName="controlD" />
            <Binding ElementName="controlE" />
            ...
        </MultiBinding>
    </MyControl.NotifyControl>
</MyControl>

如果 N 发生变化,则可以选择添加 ObservableCollection<> 。到您添加/删除控件的类,然后绑定到它(同样,使用转换器)

<MyControl NotifyControl="{Binding ElementName=ParentElement, Path=MyObservableCollection, Converter=...}" />

If N is fixed, you can use a MultiBinding (with a converter):

<MyControl>
    <MyControl.NotifyControl>
        <MultiBinding Converter="...">
            <Binding ElementName="controlA" />
            <Binding ElementName="controlB" />
            <Binding ElementName="controlC" />
            <Binding ElementName="controlD" />
            <Binding ElementName="controlE" />
            ...
        </MultiBinding>
    </MyControl.NotifyControl>
</MyControl>

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)

<MyControl NotifyControl="{Binding ElementName=ParentElement, Path=MyObservableCollection, Converter=...}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文