WPF-在itemscontrol中对齐内容

时间:2014-11-20 18:38:42

标签: c# wpf xaml scrollview itemscontrol

我在滚动查看器中有一个itemscontrol,当元素靠近屏幕中心时,它会变大。问题是,当它变大时,它的左上角被锁定到位,它看起来像这样。

Not what I want

我希望它看起来像这样,但我不知道如何去做。

What I do want

编辑:添加了xaml信息,如果需要,请询问C#(我对此表示怀疑)

<TextBlock Name="text2" Text="hello" Margin="0,15,0,-10"/>
    <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled"         Height="300" Name="Viewr" Canvas.Top="120" ScrollChanged="Viewr_ScrollChanged">
        <ItemsControl Name="viewrcontent">
            <Canvas Width="100" Height="100" Background="Red"/>
            <Canvas Width="100" Height="100" Background="Red"/>
            <Canvas Width="100" Height="100" Background="Red"/>
            <Canvas Width="100" Height="100" Background="Red"/>
            <Canvas Width="100" Height="100" Background="Orange" Name="hellobox"/>
            <Canvas Width="100" Height="100" Background="Green" Name="midbox"/>
            <Canvas Width="100" Height="100" Background="Black"/>
            <Canvas Width="100" Height="100" Background="Red"/>
            <Canvas Width="100" Height="100" Background="Red"/>
            <Canvas Width="100" Height="100" Background="Red"/>
            <Canvas Width="100" Height="100" Background="Red"/>
            <Canvas Width="100" Height="100" Background="Black"/>
            <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="FrameworkElement.Margin" Value="5"/>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" CanHorizontallyScroll="True"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </ScrollViewer>

2 个答案:

答案 0 :(得分:1)

如果您使用ScaleTransform作为RenderTransform来缩放项目,则可以将RenderTransformOrigin设置为0.5,0.5,这会强制项目围绕其中心进行缩放。

答案 1 :(得分:0)

我认为您可以编辑代码隐藏中的边距以获得所需的行为。这样的事情可以奏效:

midbox.Width += 10;
midbox.Height += 10;
midbox.Margin = new Thickness(midbox.Margin.Left - 5, midbox.Margin.Top, midbox.Margin.Right - 5, midbox.Margin.Bottom);