【问题标题】:WPF set selected item in listview when combobox is selected选择组合框时,WPF在列表视图中设置所选项目
【发布时间】:2015-07-22 14:03:36
【问题描述】:

我有以下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>

它有几个文本项和一个组合框。更改组合框值后,我想对所选项目做一些事情,这应该是与组合框刚刚交互的项目。但是,当我与项目上的组合框交互而不与项目交互时,它不会被选中。与项目的组合框交互时如何设置选定项目?

【问题讨论】:

  • 您描述的是wpf DataGrid 控件的默认行为。这个控件也更适合编辑。

标签: c# wpf listview


【解决方案1】:

Filippo 的解决方案很接近,但在我的情况下不起作用。我最终使用了他提到的TryFindParent&lt;T&gt;() 函数,但我的代码看起来像这样:

private void ComboBox_DropDownClosed(object sender, System.EventArgs e)
{
    listView.SelectedItem = null;
    var newSelectedItem = (sender as ComboBox).TryFindParent<ListViewItem>();         
    newSelectedItem.IsSelected = true;
}

listView 是我的 ListView 的名称。

【讨论】:

    【解决方案2】:

    试试这样的:

    (sender as ComboBox).TryFindParent<ListView>().SelectedItem = (sender as ComboBox).TryFindParent<ListViewItem>();
    

    在您的 dropDownClosed 事件中

    TryFindParent&lt;T&gt;() 是一个辅助函数: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);
    }
    

    【讨论】:

    • 什么是TryFindParent?这是我需要从某个地方获取的外部函数吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 2017-07-25
    • 2014-09-01
    相关资源
    最近更新 更多