【发布时间】:2014-11-20 18:38:42
【问题描述】:
我在滚动查看器中有一个 itemscontrol,当元素靠近屏幕中心时,它会变大。问题是,当它变大时,它的左上角被锁定到位,看起来像这样。
我希望它看起来像这样,但我不知道如何去做。
编辑:添加了 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>
【问题讨论】:
-
你能发布你的 xaml 吗?
-
Viewr_ScrollChanged 是做什么的?我认为这就是改变绿色项目的原因?你提供的 xaml 对我来说很好滚动,但中心项目没有增加大小或任何东西,这是你想要做的吗?
-
是的,当 Viewr_ScrollChanged 被调用时是我调整绿色项目的大小。如第一张图片所示,我可以调整画布的大小,但我不能做的是将它居中以使其看起来不难看。我可以实现 Canvas.Z 属性,但我不知道如何使元素居中。
-
如果您将
ScaleTransform用作RenderTransform来缩放项目,您可以将RenderTransformOrigin设置为0.5,0.5,这将强制项目围绕其中心进行缩放。 -
只是应该调整大小的绿色项目还是应该是中心项目?滚动离开中心后它是否应该缩小?你能把你的代码贴在后面吗?
标签: c# wpf xaml scrollview itemscontrol