开发者

Silverlight: Label does not evaluate Binding of custom DependencyProperty

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 开发者_如何学Pythonthe 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


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... :-/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜