根据您的扩展描述,没有优雅的方法来做到这一点。一个能让你接近的解决方案如下。
在MapInfo控件xaml中,从当前位置移除按钮,并将带有按钮的画布添加到GridRoot的末尾,如下所示。
<Border x:Name="MapBorder" BorderThickness="1" BorderBrush="#FF0E363C" UseLayoutRounding="False" RenderTransformOrigin="0.5,0.5" Grid.ColumnSpan="2" Grid.Column="0" Grid.RowSpan="2" Margin="0" Panel.ZIndex="1" /> <Canvas Grid.ColumnSpan="2" Background="Transparent" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Panel.ZIndex="1"> <Button x:Name="ButtonExpander" Panel.ZIndex="1" Click="ButtonExpand_Click" Height="24" Width="24" Margin="5" Canvas.Right="10" Canvas.Top="24" /> </Canvas>
这将允许您在MapInfo控件中定位按钮。
在MainWindow xaml中,处理ScrollView.ScrollChanged事件
<ListBox x:Name="LbFancy" Margin="10" Grid.Row="2" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="False" ScrollViewer.ScrollChanged="ScrollViewer_OnScrollChanged" Focusable="False" VerticalAlignment="Center" HorizontalAlignment="Center">
然后在ScrollViewer_OnScrollChanged事件处理程序中,使用类似代码管理控件中按钮的位置
Private Sub ScrollViewer_OnScrollChanged(ByVal sender As Object, ByVal e As ScrollChangedEventArgs) For Each item As MapInfo In LbFancy.Items If (item.Expanded) Then Dim positionTransform = item.TransformToAncestor(LbFancy) Dim itemPosition = positionTransform.Transform(New Point(0, 0)) If ((itemPosition.Y > 0) And (itemPosition.Y < e.ViewportHeight)) Then ' The top of the item is visible ElseIf ((itemPosition.Y < 0) And (itemPosition.Y + item.ActualHeight - button.Height > 0)) Then ' the top of the item is not visible but a part of the item is Dim button = CType(item.FindName("ButtonExpander"), Button) Canvas.SetTop(button, 0 - itemPosition.Y) End If End If Next End Sub
在控件创建和滚动期间,按钮的定位还有很多工作要做,但这应该可以让你开始。
注意:为简洁起见,已删除异常处理和空检查。
我希望这有帮助。