WPF 自定义绑定集合到非依赖属性
我创建了自己的自定义绑定类并向其添加了一个属性:
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}}}"/>
但我收到一个错误,目标不是依赖对象。有什么帮助吗?
I've created my own custom binding class and added a property to it:
public BindingGroupCollection BindingGroups
{
get { return validationResultGroup; }
set { validationResultGroup = value; }
}
public class BindingGroupCollection : ObservableCollection<BindingGroup> { }
In my xaml class i declared the objects and collection:
<local:BindingGroup x:Key="BG1"/>
<local:BindingGroup x:Key="BG2"/>
<local:BindingGroupCollection x:Key="BindingGroups1">
<StaticResourceExtension ResourceKey="BG1"/>
<StaticResourceExtension ResourceKey="BG2"/>
</local:BindingGroupCollection>
And i want to use this in my binding eg.:
<TextBox Text="{local:CustomBinding BindingGroups={Binding Source={StaticResource BindingGroups1}}}"/>
But i get an error that the target is not a dependeny object. Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能这样做,因为
Binding
不是DependencyObject
,因此它不能具有依赖属性。但是,在您的情况下,您不需要绑定,可以直接使用
StaticResource
:You can't do that, because a
Binding
isn't aDependencyObject
, so it can't have have dependency properties.However, in your case you don't need a binding, you can use the
StaticResource
directly: