WPF/Silverlight Xaml 自定义ContentControl内容符号名的问题

在silverlight中,我自己写一个继承自ContentControl的内容控件GroupBox
其xaml如下

 <ContentControl x:Class="EnvSafe.PTA.Shell.GroupBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Width="{Binding Path=Width}" Height="{Binding Path=Height}" Margin="{Binding Path=Margin}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <TextBlock Text="{Binding Path=Header}" Margin="10,0,0,0" Height="auto" HorizontalAlignment="Left" VerticalAlignment="Top" Width="auto"/>
        <Border Background="{Binding Path=Background}" Margin="0,2.5,0,0" CornerRadius="5" Padding="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Black" BorderThickness="1">
            <Grid>
                <ContentPresenter x:Name="Host" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Content="{Binding Path=Content}"/>
            </Grid>
        </Border>
    </Grid>
</ContentControl>

在程序主窗体里使用如下(片段):

 <Grid>
    <self:GroupBox Header="测试">
        <TextBlock x:Name="TbTest" />
    </self:GroupBox>
</Grid>

现在在主窗体的前台代码中,访问TbTest时会发现TbTest为null,这是由于自定义内容控件的xaml和主窗体不处于同一name scope,那么如何修改可以使得在主窗体中根据x:Name发现自定义控件的内容?

注意:为什么Grid,StackPanel,Border这些原生控件能够支持内容获取?

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问