C# 双击控件,焦点有时候并不会在此控件,所以需要根据控件是否与鼠标位置相同来进行判断此操作。

8 篇文章 0 订阅

说明:本项目采取C#WPF设计模式,业务逻辑与页面分离。现在以ListBox为例

<!--页面绑定ListBox-->

<ListBox Grid.Row="1" x:Name="strangerList" ItemsSource="{Binding StrangerList}"

                                SelectedItem="{Binding CurrentStranger,Mode=TwoWay}"
                                Margin="6,0,0,0"
                                Background="Transparent"
VerticalAlignment="Stretch"
                                HorizontalAlignment="Stretch"
BorderThickness="0"  FontSize="14"  >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseDoubleClick">
                    <i:InvokeCommandAction Command="{Binding CommandSSMouseDouble}" CommandParameter="{Binding .,ElementName=strangerList}"/>
                </i:EventTrigger>

            </i:Interaction.Triggers>

            <ListBox.ItemTemplate>

            </ListBox.ItemTemplate>

</ListBox>

           //后台cs代码

        /// <summary>
        /// 双击事件
        /// </summary>
        /// <param name="obj"></param>
        private void CommandSSMouseDouble(object obj)
         {            
              if (obj == null) return;

              var result = GetSelectItem(obj as telerik.RadListBox);//获取到控件绑定的值

        }

         /// <summary>
        /// 获取到该控件
        /// </summary>
        /// <param name="list">参数RadListbox</param>
        /// <returns></returns>
        private telerik.RadListBoxItem GetSelectItem(telerik.RadListBox list)
        {
            if (list == null) return null;
            var position = Mouse.GetPosition(list);
            IInputElement obj1 = list.InputHitTest(position);
            if (obj1 == null) return null;
            UIElement obj2 = obj1 as UIElement;
            if (obj2 == null) return null;
            return obj2.TryFindParent<telerik.RadListBoxItem>();

        }

        /// <summary>
        /// 获取控件的父节点(递归)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="child">当前双击作用于的控件</param>
        /// <returns></returns>

        public static T TryFindParent<T>(this DependencyObject child) where T : DependencyObject
        {
            //获取父项
            DependencyObject parentObject = GetParentObject(child);

            //we've reached the end of the tree
            if (parentObject == null) return null;

            //检查父项是否符合我们要找的控件类型
            T parent = parentObject as T;
            if (parent != null)
            {
                return parent;
            }
            else
            {
                //使用递归来继续下一个级别
                return TryFindParent<T>(parentObject);
            }
        }

        /// <summary>
        /// 获取控件的父节点
        /// </summary>
        /// <param name="child">当前双击作用于的控件</param>
        /// <returns></returns>
        public static DependencyObject GetParentObject(this DependencyObject child)
        {
            if (child == null) return null;
            //分别处理内容元素
            ContentElement contentElement = child as ContentElement;
            if (contentElement != null)
            {
                DependencyObject parent = ContentOperations.GetParent(contentElement);
                if (parent != null) return parent;


                FrameworkContentElement fce = contentElement as FrameworkContentElement;
                return fce != null ? fce.Parent : null;

            }

            //尝试在框架元素(如DockPanel等)中搜索父项,

            FrameworkElement frameworkElement = child as FrameworkElement;

            if (frameworkElement != null)
            {
                DependencyObject parent = frameworkElement.Parent;
                if (parent != null) return parent;
            }
            //如果它不是ContentElement / FrameworkElement,则依赖于VisualTreeHelper
            return VisualTreeHelper.GetParent(child);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值