Nested Controls and properties
I have a UserControlB which exposes a Title property of type string. I then have a UserControlA which contains one UserControlB and exposes a property called MyNestedControl which returns the instance of UserControlB.
In my main page I'm declaring a UserControlA and I'm trying to set the value of the Title property as follow MyNestedControl.Title="ABC".
However, Visual Studio throws a compile error saying that Title does not exist on the Type UserControlA.
Is this type of nesting possible through xaml? Please see below the full code.
Many thanks, Bruno
<UserControlB>
<TextBlock x:Name="txtBlock" />
</UserControlB>
public partial class UserControlB : UserControl {
public string Title
{
get { return this.txtBlock.Text; }
set { this.txtBlock.Text = value; }
}
}
<UserControlA>
<local:UserControlB x:Name="userControlB" />
</UserControlA>
public partial class UserControlA : UserControl {
public UserControlB MyNestedControl
{
get { return this.us开发者_运维技巧erControlB; }
}
}
<MainPage>
<local:UserControlA x:Name="userControlA" MyNestedControl.Title="ABC" />
</MainPage>
You're using the syntax for attached properties to try to set a 'nested' property.
Silverlight is looking for a 'Title' property defined in the 'MyNestedControl' class for the 'UserControlA' class.
This type is nesting is not possible (nor desireable - you're violating the law of demeter) in Xaml.
I think that you have to make the properties you want accessible through xaml DependencyProperties.
Try to use CustomControl insted UserControl.
精彩评论