【问题标题】:WPF Custom binding Collection to non dependency propertyWPF自定义绑定集合到非依赖属性
【发布时间】:2011-04-08 10:10:46
【问题描述】:

我已经创建了自己的自定义绑定类并为其添加了一个属性:

public BindingGroupCollection BindingGroups
{
    get { return validationResultGroup; }
    set { validationResultGroup = value; }
}

public class BindingGroupCollection : ObservableCollection<BindingGroup> { } 

在我的 xaml 类中,我声明了对象和集合:

<local:BindingGroup x:Key="BG1"/>
<local:BindingGroup x:Key="BG2"/>

<local:BindingGroupCollection x:Key="BindingGroups1">
   <StaticResourceExtension ResourceKey="BG1"/>
   <StaticResourceExtension ResourceKey="BG2"/>
</local:BindingGroupCollection>

我想在我的绑定中使用它,例如:

<TextBox Text="{local:CustomBinding BindingGroups={Binding Source={StaticResource BindingGroups1}}}"/>

但我得到一个错误,即目标不是依赖对象。有什么帮助吗?

【问题讨论】:

    标签: wpf binding dependency-properties


    【解决方案1】:

    你不能这样做,因为Binding 不是DependencyObject,所以它不能有依赖属性。

    但是,在您不需要绑定的情况下,您可以直接使用StaticResource

    <TextBox Text="{local:CustomBinding BindingGroups={StaticResource BindingGroups1}}"/>
    

    【讨论】:

    • 其实我也试过了。但是我收到一个错误:“在解析标记扩展时遇到类型 'MS.Internal.Markup.MarkupExtensionParser+UnknownMarkupExtension' 的未知属性 'ValidationResultGroups'。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    相关资源
    最近更新 更多