wpf 控件空白区域不响应事件

因为部分控件默认是Background="{x:Null}",无法获取到鼠标点击的EventArgs(就相当于空的面板)。 
设置成Background="Transparent" 透明色就可以实现了。

判断当前鼠标位置是否在DataGrid当前行


        public static bool isItemSelected = false;
        private void xStepDataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            object item = GetElementFromPoint((ItemsControl)sender, e.GetPosition((ItemsControl)sender));
            isItemSelected = (item != null);
        }

        private object GetElementFromPoint(ItemsControl itemsControl, Point point)
        {
            UIElement element = itemsControl.InputHitTest(point) as UIElement;
            while (element != null)
            {
                if (element == itemsControl)
                    return null;
                object item = itemsControl.ItemContainerGenerator.ItemFromContainer(element);
                if (!item.Equals(DependencyProperty.UnsetValue))
                    return item;
                element = (UIElement)VisualTreeHelper.GetParent(element);
            }
            return null;
        }

 

你可能感兴趣的:(WPF,c#开发,WPF开发,wpf)