Silverlight:标签不评估自定义 DependencyProperty 的绑定
我有一个 Silverlight 3 标签,我使用标签的目标属性将其连接到组合框。根据 MSDN,Label 类迭代目标绑定并搜索源中的元数据以确定 Label 的内容。
只要目标是标准控件,这实际上就有效。但是,如果我使用自定义控件(在我的例子中它扩展了 ComboBox)并引入了新的 DependencyProperty,则它会被忽略。
例如,这有效:
<dataInput:Label Grid.Row="3" Grid.Column="0"
Target="{Binding ElementName=cbxCountry}"
VerticalAlignment="Center"/>
<ComboBox x:Name="cbxCountry" DisplayMemberPath="Name"
SelectedItem="{Binding DataModel.Country, Mode=TwoWay}"
ItemsSource="{Binding Countries, Source={StaticResource ApplicationData}}"/>
在上面的示例中,搜索了 SelectedItem Binding,并且 DataModel.Country 确实包含所采用的 DisplayName。
但这不是:
<dataInput:Label Grid.Row="3" Grid.Column="0"
Target="{Binding ElementName=cbxCountry}"
VerticalAlignment="Center"/>
<local:MyComboBox x:Name="cbxCountry" DisplayMemberPath="Name"
MySelectedItem="{Binding DataModel.Country, Mode=TwoWay}"
MyItemsSource="{Binding Countries, Source={StaticResource ApplicationData}}"/>
自定义属性是依赖属性,并声明如下:
private static readonly DependencyProperty MySelectedItemProperty =
DependencyProperty.Register("MySelectedItem",
typeof(object), typeof(MyComboBox),
new PropertyMetadata(null,
MySelectedItemPropertyChanged));
我知道我可以通过在标签上定义 PropertyPath 来解决这个问题,但如果可能的话,我宁愿避免这种情况。
所以我现在的问题是,任何人都可以重现这个问题,当然更重要的是,有人知道如何解决它吗? :-)
谢谢,马库斯
I have a Silverlight 3 Label which I connect to a ComboBox using the Target Property of the Label. According to MSDN the Label class iterates through the targets bindings and searches the sources for meta data to determine the content of the Label.
This actually works as long as the target is a standard control. But if I use a custom control, which in my case extends ComboBox, and introduce a new DependencyProperty it is simply ignored.
E.g. this works:
<dataInput:Label Grid.Row="3" Grid.Column="0"
Target="{Binding ElementName=cbxCountry}"
VerticalAlignment="Center"/>
<ComboBox x:Name="cbxCountry" DisplayMemberPath="Name"
SelectedItem="{Binding DataModel.Country, Mode=TwoWay}"
ItemsSource="{Binding Countries, Source={StaticResource ApplicationData}}"/>
In the above example the SelectedItem Binding is searched and DataModel.Country does contain the DisplayName which is taken.
But this does not:
<dataInput:Label Grid.Row="3" Grid.Column="0"
Target="{Binding ElementName=cbxCountry}"
VerticalAlignment="Center"/>
<local:MyComboBox x:Name="cbxCountry" DisplayMemberPath="Name"
MySelectedItem="{Binding DataModel.Country, Mode=TwoWay}"
MyItemsSource="{Binding Countries, Source={StaticResource ApplicationData}}"/>
The custom Properties are Dependency Properties and declared as follws:
private static readonly DependencyProperty MySelectedItemProperty =
DependencyProperty.Register("MySelectedItem",
typeof(object), typeof(MyComboBox),
new PropertyMetadata(null,
MySelectedItemPropertyChanged));
I know that I can work around this by defining the PropertyPath on the Label, but I'd rather avoid this, if possible.
So my question now is, can anyone reproduce this issue, and much more important of course, does anybody know how to solve it? :-)
Thanks, Markus
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,如果有人遇到同样的问题,解决方案如下:只需将 DependencyProperty 的可见性从私有更改为公共即可。
实际上很明显......:-/
Ok, in case anyone runs into the same problem, here is the solution: Just change the visibility of the DependencyProperty from private to public.
Quite obvious actually... :-/