使用ContentPresenter自定义UserControl,嵌套对象为null

时间:2012-06-13 15:00:42

标签: .net user-controls contentpresenter templatebinding targetinvocationexception

我有一个自定义的UserControl(CustomControl1),我在MainPage.xaml中用作数据容器。
作为MainPage- CustomControl1的内容,我放置了一些按钮。

我的错误/问题:
将显示按钮,但是当我尝试设置按钮属性时出现错误 Button对象将为null ....但为什么呢? 此外,当我在开发环境中单击它时,不会从嵌套对象中引发任何事件。只有当我在XAML中选择嵌套对象时,我才能使用[Object] - > Properties-> Events。

Silverlight来源:
Vers.4
Vers.6 with TemplateBinding and Error

我必须改变什么?
我想,这可能是我在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>

我的CustomControl1.xaml.cs:

    public UIElement NewContent
    {
        get { return (UIElement)GetValue(NewContentProperty); }
        set { SetValue(NewContentProperty, value); }
    }

    public static readonly DependencyProperty NewContentProperty =
        DependencyProperty.Register("NewContent", typeof(UIElement), typeof(CustomControl1), null);

    public string Button2Content
    {
        set
        {
            // On run time myButton2 and Button2 are null
            // Because LayoutRoot_Loaded isn't finished.
            ((Button)LayoutRoot.FindName("Button2")).Content = value;
        }
        get
        {
            return myButton2.Content.ToString();
        }
    }

    private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
    {
        //Next step would throw an error, because Button1 is null ... Why?
        //Button1.Content = "NewContent"; 
        //Maybe a problem with ContentPresenter binding in CustomControl1?

        // This works ... But a little complicated...
        myButton1 = (Button)LayoutRoot.FindName("Button1");
        myButton2 = (Button)LayoutRoot.FindName("Button2");
        myButton3 = (Button)LayoutRoot.FindName("Button3");
        //myButton1.Content = "NewContent";
    }

我的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>

0 个答案:

没有答案