WPF ItemsControl Uncheck the item item and scroll bar automatically runs to the top of the item

Source: Internet
Author: User

In the project ItemsControl custom DataTemplate, the code is as follows:

<scrollviewer x:name= "Paperscrollciewer" margin= "1" horizontalscrollbarvisibility= "Auto" Verticalscrollbarvisibility= "Auto" background= "#ccc" scrollchanged= "paperscrollciewer_scrollchanged" > <I Temscontrol x:name= "Mainitemscontrol" > <ItemsControl.ItemsPanel> <itemspanel Template> <StackPanel/> </ItemsPanelTemplate> <                        /itemscontrol.itemspanel> <ItemsControl.ItemTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> < RowDefinition height= "Ten" ></RowDefinition> <rowdefinition height= "10*" ></ rowdefinition> </Grid.RowDefinitions> <inkcanvas grid.ro w= "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>

Where InkCanvas is used to add ink, the function is to implement loading a lot of canvas, each canvas can be drawn separately ink. When the runtime finds that the first time a canvas is clicked, the scrollbar is automatically adjusted to the top of the item, and the conjecture is triggered by the SelectedChange event, which needs to be aligned by default when the item is selected.

The workaround is simple, set the property cancontentscroll= "True" for ItemsControl, and solve it perfectly.

The modified code is as follows:

<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&gt                ;                        </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate>                                <Grid> <Grid.RowDefinitions> <rowdefinition height= "Ten" ></RowDefinition> <rowdefinition height= "10*" >& lt;/rowdefinition> </Grid.RowDefinitions> <inkcanvas Gri d.row= "1" background= "{Binding Imgpath,converter={staticresource Bitmapsourceconvert}}" strokes= "{Binding Strokes}" Width= "{Binding Width}" height= "{Binding Height}" > <inkcanvas.defaultdrawingattribute        S>                            <drawingattributes color= "#FFFB1818" fittocurve= "false" height= "3" ignorepressure= "false" I Shighlighter= "False" stylustip= "Ellipse" stylustiptransform= "Identity" width= "3"/> </                    Inkcanvas.defaultdrawingattributes> </InkCanvas> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> </itemscontrol&gt        ; </ScrollViewer>

  

WPF ItemsControl Uncheck the item item and scroll bar automatically runs to the top of the item

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.