简体   繁体   English

样式中依赖项属性的绑定集合类型的绑定问题

[英]Issue with binding Collection type of dependency property in style

I have a customcontrol exposing a Dependency property of type ObservableCollection. 我有一个customcontrol公开类型为ObservableCollection的Dependency属性。 When i bind this properrty directly as part ofthe control's mark up in hte containing control everythihng works fine 当我将此属性直接绑定为包含控件的控件标记的一部分时,一切都可以正常工作

<temp:EnhancedTextBox 
    CollectionProperty="{Binding Path=MyCollection, Mode=TwoWay}"/>

But when i try to do the binding in the style created for the control it fails, 但是,当我尝试以为控件创建的样式进行绑定时,它将失败,

<Style x:Key="abc2" 
   TargetType="{x:Type temp:EnhancedTextBox}" > 
    <Setter Property="CollectionProperty" 
        Value="{Binding Path=MyCollection, Mode=TwoWay}"/>
</Style>

Please help !!!!! 请帮忙 !!!!!

Thanks 谢谢

It has to do with your data context for the style. 它与样式的数据上下文有关。 There is no way for the style to know where MyCollection is coming from because although you may have it defined in the same file, the style does not share the data context. 样式无法知道MyCollection的来源,因为尽管您可能在同一个文件中定义了MyCollection,但是样式并不共享数据上下文。

I would also ask the question as to why you are setting the property in the style? 我还要问一个问题,为什么要设置样式的属性? The style is not meant for this sort of operation. 该样式并不适合此类操作。 The style is supposed to control the look of the UI element not provide the functionality. 该样式应该控制UI元素的外观,但不提供该功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM