说明:本项目采取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);
}