【问题标题】:Dependency Property to fill list or array填充列表或数组的依赖属性
【发布时间】:2011-07-27 17:33:09
【问题描述】:

我有一个自定义控件。这有能力与其他几个控件一起做某事。我希望它有一个元素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)和Test(类型String)。

正如我所说,我的列表有两个条目,两个 NotifyControlWrapper。但是虽然Test 包含给定的字符串,但Viewnull。为什么会这样或如何调试?

【问题讨论】:

  • 不太清楚您要做什么,但在我看来应该涉及 ObservableCollection?
  • 不是那样的,因为运行时没有变化。但是我在不同的窗口中重复使用该控件,并且每次它都应该突出显示一个或多个其他控件。它适用于 NotifyControl1="{Binding ElementName=a}" NotifyControl2="{Binding ElementName=b}",但这不像任何好的编程。

标签: wpf xaml dependency-properties


【解决方案1】:

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

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

ControlWrapper 将只有一个成员属性 Control,以便您可以指定绑定。

【讨论】:

  • 所以NotifyControls 是一个List&lt;ControlWrapper&gt;ControlWrapper 是一个继承自DependencyObject 的类?
  • 我会使用 List,是的,但 ControlWrapper 不必从 DependencyObject(或任何东西)继承。它可以只是类 ControlWrapper { Control Control { get;放; } }
  • 这看起来不错。但是我有 ControlWrapper 来继承 DependencyObject,否则 Control 不能是 DependencyProperty。但这没有问题。问题是,控制属性没有设置。访问列表时,我得到正确数量的 ControlWrapper 条目,如果我添加一个字符串属性Test,它的内容是正确的。但是Control 总是空的。在这种情况下必须以任何特殊方式指定绑定吗?
  • 您是让 ControlWrapper 成为 DependencyObject 并让 Control 成为 DependencyProperty,还是使用单向绑定?您是否在调试器的输出窗口中收到任何绑定错误?
  • 您不能自动将元素绑定到界面。将 View 更改为 UIElement,或使用值转换器转换为 ICustomerView。
【解决方案2】:

如果 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=...}" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多