问题描述
- 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=""10000"" Height=""auto"" HorizontalAlignment=""Left"" VerticalAlignment=""Top"" Width=""auto""/> <Border Background=""{Binding Path=Background}"" Margin=""02.500"" 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这些原生控件能够支持内容获取?
解决方案
http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD%E7%9F%A5%E8%AF%86%E5%BA%93/841.shtml
时间: 2024-11-03 13:47:26