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> ; </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> ; </ScrollViewer>
WPF ItemsControl Uncheck the item item and scroll bar automatically runs to the top of the item