开发者

UserControl ContentProperty, Content objects are null

开发者 https://www.devze.com 2023-04-08 03:31 出处:网络
I created a custom silverlight UserControl.I need to be able to set its content through a \"Child\" property.So I used the \"[ContentProperty(\"Child\")]\" class attribute :

I created a custom silverlight UserControl. I need to be able to set its content through a "Child" property. So I used the "[ContentProperty("Child")]" class attribute :

[ContentProperty("Child")] 
public partial class SizeableCheckBox : UserControl
{
    public SizeableCheckBox()
    {
        InitializeComponent();
    }

    public object Child
    {
        get { return contentControl1.Content; }
        set { contentControl1.Content = value; }
    }

The XAML of the UserControl looks like that :

<Grid x:Name="LayoutRoot" >
    <StackPanel Orientation="Horizontal">
        <Border x:Name="brdCheck" />
        <ContentControl x:Name="contentControl1" />
    </StackPanel
    ...
</Grid>

Now if I use my UserControl in my application everything works fine (even in VS2010 design mode) :

<my:SizeableCheckBox x:Name="chkTestCheck">
    <StackPanel Orientation="Horizontal">
        <Image ... />
        <Textblock x:Name="txtCheckBoxTest" Text="My 开发者_C百科Checkbox test" />
    </StackPanel>
</my:sizeableCheckBox>

But in my code I have a reference to the "txtCheckBoxTest" but that object is null on runtime. What am I doing wrong?

Thanks


you should be able to go chkTestCheck.txtCheckBoxTest.Text unless i am misunderstanding the quesiton

0

精彩评论

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