[英]WPF set selected item in listview when combobox is selected
我有以下listview
:
<ListView Grid.Row="1" ItemsSource="{Binding Transducers}" SelectedItem="{Binding SelectedTransducer}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding LabID}"/>
<GridViewColumn Header="Manufacturer" DisplayMemberBinding="{Binding Manufacturer}"/>
<GridViewColumn Header="Channel">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataContext.Channels, ElementName=mainInterface}"
SelectedItem="{Binding Channel, Mode=TwoWay}"
SelectedValue="{Binding Channel.ID}"
SelectedValuePath="ID"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
它具有一些文本項和一個組合框。 更改組合框值后,我想對所選項目進行操作,該項目應該是剛剛與組合框進行交互的項目。 但是,當我與該項目上的組合框交互,而與該項目沒有其他交互時,它不會被選中。 與該項目的組合框交互時如何設置所選項目?
嘗試這樣的事情:
(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);
}
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的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.