.net 使用ContentPresenter自定义UserControl,嵌套对象为空
我有一个自定义的UserControl(CustomControl1),我在MainPage.xaml中将其用作数据容器。.net 使用ContentPresenter自定义UserControl,嵌套对象为空,.net,user-controls,contentpresenter,templatebinding,targetinvocationexception,.net,User Controls,Contentpresenter,Templatebinding,Targetinvocationexception,我有一个自定义的UserControl(CustomControl1),我在MainPage.xaml中将其用作数据容器。 作为主页-CustomControl1的内容,我放置了一些按钮 我的错误/问题: 按钮将被显示,但我在尝试设置按钮属性时出错。 按钮对象将为空…。但是为什么呢? 此外,当我在开发环境中单击嵌套对象时,不会从嵌套对象引发任何事件。只有在XAML中选择嵌套对象,我才能使用[object]->Properties->Events 银色光源: 我必须改变什么? 我想,这可能是C
作为主页-CustomControl1的内容,我放置了一些按钮 我的错误/问题:
按钮将被显示,但我在尝试设置按钮属性时出错。
按钮对象将为空…。但是为什么呢?
此外,当我在开发环境中单击嵌套对象时,不会从嵌套对象引发任何事件。只有在XAML中选择嵌套对象,我才能使用[object]->Properties->Events 银色光源:
我必须改变什么?
我想,这可能是CustomControl1中的ContentPresenter绑定错误 我发现很多页面都有这样的问题。。。但我无法纠正我的错误 我的CustomControl1.xaml:
<UserControl.Resources>
<Style x:Key="roundBorderStyle" TargetType="Border">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF5F0E8E" Offset="0"/>
<GradientStop Color="#FFA6CEF0" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="BorderBrush" Value="#FF0F2048"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
</Style>
</UserControl.Resources>
<Border Width="Auto" Style="{StaticResource roundBorderStyle}">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Height="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<sdk:Label x:Name="FrameCaption" Content="Caption" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.Row="0" />
<Button x:Name="OpenButton" Content="Click" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,4,10,0" />
</Grid>
<ContentPresenter Grid.Row="1" x:Name="FrameContent"
Content="{Binding NewContent, ElementName=userControl}"
Margin="4" Width="Auto" Height="Auto"/>
</Grid>
</Border>
My MainPage.xaml:
<my:CustomControl1 x:Name="LayoutRoot" Button2Content="New2Content" Loaded="LayoutRoot_Loaded">
<StackPanel>
<Button Content="Button 1" x:Name="Button1"/>
<Button Content="Button 2" x:Name="Button2"/>
<Button Content="Button 3" x:Name="Button3"/>
</StackPanel>
</my:CustomControl1>
只是一个提示,因为我不确定确切的解决方案:LayoutRoot.FindName()在DataTemplates中找不到控件。也许VisualTreeHelper.GetChild()可以做到这一点(也许您必须递归使用它),谢谢!是的,这就像FindName。我添加了一个Vers.4 Silverlight解决方案。我想有一个直接使用嵌套对象的可能性。。。但恐怕不可能。请看这篇文章。希望这能有所帮助:不幸的是,当我试图设置CountDisplayControl.TestoverTemplateBinding时,我遇到了一个新错误System.Reflection.TargetInvocationException。请参见Vers.6->Generic.xaml->
<my:CustomControl1 x:Name="LayoutRoot" Button2Content="New2Content" Loaded="LayoutRoot_Loaded">
<StackPanel>
<Button Content="Button 1" x:Name="Button1"/>
<Button Content="Button 2" x:Name="Button2"/>
<Button Content="Button 3" x:Name="Button3"/>
</StackPanel>
</my:CustomControl1>