我有以下g++ -o pathfinder main.o OpenCL.o -L/opt/AMDAPP/lib/x86_64/ -lOpenCL -lrt
:
listview
它有一些文本项和一个组合框。在我更改组合框值后,我想对所选项目执行某些操作,该项目应该是其组合框刚刚与之交互的项目。但是,当我与项目上的组合框进行交互而没有与项目进行任何其他操作时,它不会被选中。当我与该项目的组合框进行交互时,如何设置所选项目?
答案 0 :(得分:0)
尝试这样的事情:
(sender as ComboBox).TryFindParent<ListView>().SelectedItem = (sender as ComboBox).TryFindParent<ListViewItem>();
你的dropDownClosed事件上的
TryFindParent<T>()
是一个帮助函数,可在此处找到:http://www.hardcodet.net/2008/02/find-wpf-parent
/// <summary>
/// Finds a parent of a given item on the visual tree.
/// </summary>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="child">A direct or indirect child of the
/// queried item.</param>
/// <returns>The first parent item that matches the submitted
/// type parameter. If not matching item can be found, a null
/// reference is being returned.</returns>
public static T TryFindParent<T>(this DependencyObject child)
where T : DependencyObject
{
//get parent item
DependencyObject parentObject = GetParentObject(child);
//we've reached the end of the tree
if (parentObject == null) return null;
//check if the parent matches the type we're looking for
T parent = parentObject as T;
if (parent != null)
{
return parent;
}
else
{
//use recursion to proceed with next level
return TryFindParent<T>(parentObject);
}
}
/// <summary>
/// This method is an alternative to WPF's
/// <see cref="VisualTreeHelper.GetParent"/> method, which also
/// supports content elements. Keep in mind that for content element,
/// this method falls back to the logical tree of the element!
/// </summary>
/// <param name="child">The item to be processed.</param>
/// <returns>The submitted item's parent, if available. Otherwise
/// null.</returns>
public static DependencyObject GetParentObject(this DependencyObject child)
{
if (child == null) return null;
//handle content elements separately
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;
}
//also try searching for parent in framework elements (such as DockPanel, etc)
FrameworkElement frameworkElement = child as FrameworkElement;
if (frameworkElement != null)
{
DependencyObject parent = frameworkElement.Parent;
if (parent != null) return parent;
}
//if it's not a ContentElement/FrameworkElement, rely on VisualTreeHelper
return VisualTreeHelper.GetParent(child);
}
答案 1 :(得分:0)
Filippo的解决方案很接近,但在我的情况下不起作用。我最终使用了他提到的TryFindParent<T>()
函数,但我的代码看起来像这样:
private void ComboBox_DropDownClosed(object sender, System.EventArgs e)
{
listView.SelectedItem = null;
var newSelectedItem = (sender as ComboBox).TryFindParent<ListViewItem>();
newSelectedItem.IsSelected = true;
}
其中listView
是我的ListView的名称。