码迷,mamicode.com
首页 > Windows程序 > 详细

WPF ItemsControl 取消选中item项,滚动条自动跑到该item顶部的问题

时间:2016-09-01 10:53:03      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

项目中ItemsControl 自定义了DataTemplate,代码如下:

<ScrollViewer x:Name="PaperScrollCiewer"  Margin="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Background="#ccc" ScrollChanged="PaperScrollCiewer_ScrollChanged" >
            <ItemsControl x:Name="MainItemsControl">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="10"></RowDefinition>
                                <RowDefinition Height="10*"></RowDefinition>
                            </Grid.RowDefinitions>
                            <InkCanvas Grid.Row="1" Background="{Binding ImgPath,Converter={StaticResource BitmapSourceConvert}}" Strokes="{Binding Strokes}" Width="{Binding Width}" Height="{Binding Height}">
                                <InkCanvas.DefaultDrawingAttributes>
                                    <DrawingAttributes Color="#FFFB1818" FitToCurve="False" Height="3" IgnorePressure="False" IsHighlighter="False" StylusTip="Ellipse" StylusTipTransform="Identity" Width="3"/>
                                </InkCanvas.DefaultDrawingAttributes>
                            </InkCanvas>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>

  其中InkCanvas是用来添加墨迹的,功能就是想实现加载很多个画布,每个画布能单独的绘制墨迹。运行时候发现第一次点击某一个画布,滚动条就自动调整到该item的顶部去了,猜想是触发了SelectedChange事件,默认该item选中时候需要对齐。

  解决方法很简单,给ItemsControl设置属性CanContentScroll="True",完美解决。

  修改后代码如下:

<ScrollViewer x:Name="PaperScrollCiewer"  Margin="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Background="#ccc" ScrollChanged="PaperScrollCiewer_ScrollChanged" CanContentScroll="True">
            <ItemsControl x:Name="MainItemsControl">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="10"></RowDefinition>
                                <RowDefinition Height="10*"></RowDefinition>
                            </Grid.RowDefinitions>
                            <InkCanvas Grid.Row="1" Background="{Binding ImgPath,Converter={StaticResource BitmapSourceConvert}}" Strokes="{Binding Strokes}" Width="{Binding Width}" Height="{Binding Height}">
                                <InkCanvas.DefaultDrawingAttributes>
                                    <DrawingAttributes Color="#FFFB1818" FitToCurve="False" Height="3" IgnorePressure="False" IsHighlighter="False" StylusTip="Ellipse" StylusTipTransform="Identity" Width="3"/>
                                </InkCanvas.DefaultDrawingAttributes>
                            </InkCanvas>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>

  

WPF ItemsControl 取消选中item项,滚动条自动跑到该item顶部的问题

标签:

原文地址:http://www.cnblogs.com/tracyjfly/p/5828425.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!