wpf itemscontrol项目无法控制

时间:2011-08-25 14:46:42

标签: wpf templates itemscontrol

我有一个自己的控件,它使用自己的模板从itemscontrol派生。我在itemscontrol中使用Canvas作为ItemsPanel。为什么f.e.在调整窗口大小时,项目也可以在itemscontrol之外吗?

模板:

<Style TargetType="{x:Type local:Dashboard}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:Dashboard}">
                <Grid>
                    <Border Background="{TemplateBinding Background}" 
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

项目使用此:

<Style TargetType="{x:Type local:Widget}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:Widget}">
                <Grid Background="{TemplateBinding Background}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>

                    <Border Grid.Row="0" BorderThickness="2" BorderBrush="Black" Background="WhiteSmoke"
                            x:Name="Part_Header">
                        <ContentPresenter ContentSource="Header"/>
                    </Border>
                    <Border Grid.Row="1" BorderThickness="2" BorderBrush="Black" Background="WhiteSmoke">
                        <Grid>
                            <ContentPresenter />

                            <ResizeGrip x:Name="Part_Resize" 
                                HorizontalAlignment="Right"
                                VerticalAlignment="Bottom"
                                Cursor="SizeNWSE" />
                        </Grid>

                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:0)

根据您的对齐和边距设置,调整父级的大小可能会导致子级移动到父级边界之外。我发现检查这个的最简单方法是加载Blend并调整父级的大小,观察包含控件的移动方式。通过调整Blend中的锚点(更改对齐和边距),您应该能够解决它们移动的原因。