WPF:如何将usercontrols分组到一个集合中

时间:2016-05-04 21:32:22

标签: wpf xaml user-controls datacontext

我有Usercontrol来电ChartBase,我可以在其上放置各种轴UserControls。如果我像这样使用它,一切都很好:

<UserControl x:Class="Chart_1.ChartBase"
             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:Chart_1"
             mc:Ignorable="d" 
             x:Name="ThisControl"
             d:DesignHeight="700" d:DesignWidth="1050">

<Grid 
    x:Name="LayoutRoot" 
    DataContext="{Binding ElementName=ThisControl}"
    Height="{Binding ActualHeight}"
    Width="{Binding ActualWidth}">

    <Grid.RowDefinitions>
        <RowDefinition Height="20*"/>
        <RowDefinition Height="60*"/>
        <RowDefinition Height="20*"/>
    </Grid.RowDefinitions>


    <TextBlock x:Name="TitleTB" Grid.Row="0" Text="Title" TextAlignment="Center"  Margin="20"/>


    <Viewbox Grid.Row="1" Stretch="Uniform">

        <Canvas x:Name="ChartCanvas"                    
                Background="AliceBlue"
                Height="{Binding ActualHeight}"
                Width="{Binding ActualWidth}">

            <local:RuledTopAxis
                AxisBrush="Red"
                AxisThickness="2"
                Length="{Binding ElementName=ChartCanvas, Path=ActualWidth}"
                TickBrush="Purple"
                TickSize="10"
                TickThickness="3"
                TickInterval="50"
                ChartWidth="{Binding ElementName=ChartCanvas, Path=ActualWidth}"
                ChartHeight="{Binding ElementName=ChartCanvas, Path=ActualHeight}"/>


            <local:LeftAxis 
                AxisBrush="Green"
                AxisThickness="2"
                Length="{Binding ElementName=ChartCanvas, Path=ActualHeight}"
                TickBrush="Goldenrod"
                TickSize="20"
                TickThickness="3"
                TickInterval="30"
                ChartWidth="{Binding ElementName=ChartCanvas, Path=ActualWidth}"
                ChartHeight="{Binding ElementName=ChartCanvas, Path=ActualHeight}"/>

        </Canvas>
    </Viewbox>

    <Grid x:Name="LegendSpace" Grid.Row ="2" Background="Transparent"  Margin="20" >
        <TextBlock Text="Legend space" TextAlignment="Center"/>
    </Grid>
</Grid>

我想做的是用系列AxisGroup替换那一系列的轴,这样ChartBase可以真正成为基类,我可以用不同种类的轴集合从中派生出不同的类。我尝试的是使用以下xaml定义一个名为UserControl的新AxisGroup

<ItemsControl 
    DataContext = ThisControl
    ItemsSource="{Binding Children}" />

其中Children是AxisGroup的DP,它包含一个`ObservableCollection。然后我可以通过以下方法替换ChartBase中的轴序列:

<local:AxisGroup>
    <AxisGroup.Children>
       <local:Axis_type1 
            ... 
            Length = "{Binding ElementName=ChartCanvas, Path=ActualWidth}"
            ... />
       <local:Axis_type2 ... />
    </AxisGroup.Children>
</local:AxisGroup>

当然,我们的想法不是在xaml中定义轴,而是在各种子类的代码中定义它们;但这只是为了测试。 它有点工作。我确实得到了两个轴,但不是我希望它们的位置,而且长度不合适。

问题似乎在于Axis基类的工作方式。对于所有轴,我所做的是设置一个水平的LineGeometry,添加一些指向下方的刻度和下面的一些标签。然后,在课程BottomAxis中,我设置RenderTransform将其翻译到画布的底部。对于LeftAxis,我会旋转,翻转,翻译以获得我想要的位置。这都是在基类中完成的,派生类只是设置了转换的一些参数。这适用于第一个示例,但它不适用于AxisGroup控件中的轴。我怀疑这是一个DataContext问题,但无法看清楚。

对不起它太长了...感谢您的任何想法。

1 个答案:

答案 0 :(得分:0)

当然,这与指定ItemsPanel

一样简单
<ItemsControl 
DataContext = ThisControl
ItemsSource="{Binding Children}" >
    <ItemsControl.ItemsPanel>
        <Canvas />
    </ItemsControl.ItemsPanel>
</ItemsControl>
相关问题