如何通过用户的XAML,4.0动态地向UserControl添加控件

时间:2016-08-19 12:44:55

标签: wpf xaml user-controls

我想创建一个包含Image和Conainer的用户控件,允许用户在XAML中动态地将他/她自己的控件添加到用户控件。我正在使用VS 2010,.NET 4.0

我创建了以下代码。

UserController.xaml

 <Grid DockPanel.Dock="bottom">      
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <StackPanel  Height="Auto" Grid.Row="0" Grid.Column="0">
        <Image />
    </StackPanel>
    <Grid>
        <ContentControl Content="{Binding Innercontent}" />
    </Grid>
</Grid>

ModeViewCode,Innercontent是DependencyProperty属性

public static DependencyProperty InnerContentProperty = DependencyProperty.Register("InnerContent", typeof(UIElement), typeof(Footer))

public UIElement InnerContent
    {
        get { return (UIElement)GetValue(InnerContentProperty); }
        set { SetValue(InnerContentProperty, value); }
    }

创建了一个使用usercontrol的DataTemplate。

 <users:Footer HorizontalAlignment="Stretch" VerticalAlignment="Bottom">
                <users:Footer.InnerContent>
                    <StackPanel Orientation="Horizontal" x:Name="tst">
                       <commoncontrols:Button />
                    </StackPanel>
                </users:Footer.InnerContent>
            </users:Footer>

当代码被编译时,它会给出以下错误消息。

错误275未知构建错误,'索引(基于零)必须大于或等于零且小于参数列表的大小。 2582号线位置23.'在这一行(用户:Footer.InnerContent)。

通过关注psot Help

获得帮助

任何帮助将不胜感激。

0 个答案:

没有答案