我在Canvas上有ListBoxItems。我可以使用ScrollViewer在画布上拖动和滚动视图以及一些鼠标事件。这些鼠标事件似乎覆盖了ListBox的SelectionChanged事件,该事件嵌套在ScrollViewer中,或者从未进行选择,因为鼠标事件在ListBox之前捕获了点击。如果有人对此问题有任何解决方案,将不胜感激。谢谢你的时间,这是我的代码。
XAML:
<ScrollViewer x:Name="objScrollViewer"
Grid.Row="0"
Grid.Column="1"
VerticalScrollBarVisibility="Hidden"
HorizontalScrollBarVisibility="Hidden"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
MouseLeftButtonUp ="OnMouseLeftButtonUp"
PreviewMouseLeftButtonUp ="OnMouseLeftButtonUp"
PreviewMouseLeftButtonDown="OnPreviewMouseLeftButtonDown"
PreviewMouseMove="OnPreviewMouseMove"
PreviewMouseUp="OnPreviewMouseUp"
ScrollChanged="OnScrollViewerScrollChanged">
<ListBox x:Name="objCoreViewer"
ItemsSource="{Binding ItemsSource}"
Background="Transparent"
SelectionChanged="objCoreViewer_SelectionChanged"
ItemTemplateSelector="{DynamicResource CoreViewerDataTemplateSelector}"
ItemContainerStyleSelector="{DynamicResource ItemContainerStyleSelector}"
PreviewMouseWheel="objCoreViewer_PreviewMouseWheel">
<!-- Core Map Canvas -->
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<local:CoreViewerCanvas x:Name="objCoreViewerCanvas"
Background="Transparent"
ClipToBounds="True">
<local:CoreViewerCanvas.LayoutTransform>
<ScaleTransform ScaleX="{Binding Path=Value, ElementName=ZoomSlider}"
ScaleY="{Binding Path=Value, ElementName=ZoomSlider}" />
</local:CoreViewerCanvas.LayoutTransform>
</local:CoreViewerCanvas>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</ScrollViewer>
C#:
这绝不会被称为
private void objCoreViewer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
这会被称为
private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var mousePos = e.GetPosition(objScrollViewer);
if (mousePos.X <= objScrollViewer.ViewportWidth && mousePos.Y <
objScrollViewer.ViewportHeight)
{
objScrollViewer.Cursor = Cursors.SizeAll;
lastDragPoint = mousePos;
Mouse.Capture(objScrollViewer);
}
}
编辑1:
我将ScrollViewer事件属性从PreviewMouseLeftButtonDown更改为MouseLeftButtonDown,它现在触发SelectionChanged事件方法,但ScrollViewer现在不滚动导致MouseLeftButtonDown事件方法永远不会被捕获。 SIGH!
答案 0 :(得分:0)
将鼠标事件方法更改为:
private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var mousePos = e.GetPosition(objScrollViewer);
if (mousePos.X <= objScrollViewer.ViewportWidth && mousePos.Y <
objScrollViewer.ViewportHeight)
{
objScrollViewer.Cursor = Cursors.SizeAll;
lastDragPoint = mousePos;
Mouse.Capture(objScrollViewer);
}
FrameworkElement ctrl = (e.OriginalSource as FrameworkElement);
if (ctrl != null)
{
switch (ctrl.DataContext.GetType().ToString())
{
case "GE.GNF.ACUMEN.Library.SIMULATOR_BUNDLE_NODAL_DATA":
objCoreViewer.SelectedItem = ctrl.DataContext as SIMULATOR_BUNDLE_NODAL_DATA;
break;
case "GE.GNF.ACUMEN.Library.SIMULATOR_CONTROL_BLADE_NODAL_DATA":
objCoreViewer.SelectedItem = ctrl.DataContext as SIMULATOR_CONTROL_BLADE_NODAL_DATA;
break;
default:
break;
}
}
}