开发者

PreviewMouseLeftButtonDown preventing SelectionChanged event

开发者 https://www.devze.com 2023-03-13 06:23 出处:网络
I have ListBoxItems on a Canvas. I have the ability to drag and scroll the view on the canvas with some Mouse events using a ScrollViewer. These mouse events seem to override the SelectionChanged even

I have ListBoxItems on a Canvas. I have the ability to drag and scroll the view on the canvas with some Mouse events using a ScrollViewer. These mouse events seem to override the SelectionChanged event for the ListBox which is nested within the ScrollViewer or the Selection is never made because the mouse events capture the click before the ListBox does. If anyone has any solutions to this problem it would be greatly appreciated. Thanks for your time and here is my code.

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#:

This never gets called

    private void objCoreViewer_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    }

This does get called

    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);
        }
    }

EDIT 1:

I chan开发者_JS百科ged the ScrollViewer event property from PreviewMouseLeftButtonDown to MouseLeftButtonDown and it now fires the SelectionChanged event method but the ScrollViewer does not scroll now cause the MouseLeftButtonDown event method never gets captured. SIGH!


Changed the mouse event method to:

    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;
            }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消