开发者

wpf itemscontrol items outside of control

开发者 https://www.devze.com 2023-03-31 06:05 出处:网络
I have an own control which derives from itemscontrol with an own template. I am using a Canvas inside the itemscontrol as ItemsPanel. Why f.e. on resize of the window the items also can be outside of

I have an own control which derives from itemscontrol with an own template. I am using a Canvas inside the itemscontrol as ItemsPanel. Why f.e. on resize of the window the items also can be outside of the itemscontrol?

Templates:

<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>
            &开发者_如何学Pythonlt;/ControlTemplate>
        </Setter.Value>
    </Setter>

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

The items use this:

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


Depending on your alignment and margin settings, resizing a parent can cause children to move outside of a parent's boundaries. The easiest way I've found to check this is to load Blend and resize the parent, watching how contained controls move. By tweaking the anchors in Blend (which changes alignments and margins), you should be able to troubleshoot why they move.

0

精彩评论

暂无评论...
验证码 换一张
取 消