将子控件的子控件添加到父级

时间:2016-11-22 21:13:55

标签: c# wpf

我想使用用户控件在WPF中实现类似控件的工具箱。我能够获得UCITems的所有标题,并在UCTab中显示为标签,并使用ContentProperty绑定到UCTab中的StackPanel。但我也需要UCItem儿童并绑定到UCTab中的另一个堆栈面板。

MainWindow.xaml:

<local:UCTab>
        <local:UCItem Title="ToolBox 1">
            <StackPanel>
                <TextBlock Text="1"></TextBlock>
                <TextBlock Text="2"></TextBlock>
                <TextBlock Text="3"></TextBlock>
            </StackPanel>
        </local:UCItem>
        <local:UCItem Title="ToolBox 2"></local:UCItem>
        <local:UCItem Title="ToolBox 3"></local:UCItem>

    </local:UCTab>`

UCTab UserControl

<UserControl x:Class="UserTab.UCTab "
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:UserTab"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>        
</UserControl.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <Border Name="bd" Background="Gray" Panel.ZIndex="50" >
        <StackPanel x:Name="host" >

        </StackPanel>
    </Border>
    <Border x:Name="bdCanvas" Grid.Column="1">
        <Canvas x:Name="canvas" Background="SkyBlue" >
            <StackPanel x:Name="spPanel" Orientation="Vertical" Width="180" >
                <Grid Background="Navy">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition ></ColumnDefinition>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock x:Name="tbHeader" Foreground="White" Grid.Column="0" Text="Heading" Margin="3" ></TextBlock>
                    <Button x:Name="btnPin" Content="P" Margin="3" Grid.Column="1"></Button>
                    <Button x:Name="btnClose" Content="X" Margin="3" Grid.Column="2"></Button>
                </Grid>

                <StackPanel x:Name="panelUCItem"  > 

                </StackPanel>

            </StackPanel>
        </Canvas>
    </Border>
</Grid>

UCITem UserCotrol:

<UserControl x:Class="UserTab.UCITem "
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:UserTab"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <Style TargetType="{x:Type UCITem }">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type UCITem }">
                    <Border Name="Border"
                        Background="Gray"
                        Margin="5">
                        <Button x:Name="tbItem" Padding="5" Width="100" Height="28" Foreground="Black" Content="{Binding Title,UpdateSourceTrigger=PropertyChanged}">
                            <Button.LayoutTransform>
                                <RotateTransform Angle="90"></RotateTransform>
                            </Button.LayoutTransform>
                        </Button>
                    </Border>


                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

点击UCItem按钮,在UCTab中显示UCItem内容Stackpanel'panelUCItem'。

帮助我如何继续这个结构。感谢

0 个答案:

没有答案