Heroius 2015-12-22 11:01 采纳率: 0%
浏览 1853

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条回答 默认 最新

  • devmiao 2016-02-07 23:35
    关注
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP