简体   繁体   English

wpf-如何在GridView单元格中找到元素的ListView行?

[英]wpf - How do I find the ListView row of an element in a GridView cell?

I have a combobox embedded within a grid view cell that is subsequently embedded in ListView object. 我在网格视图单元格中嵌入了一个组合框,该组合框随后又嵌入到ListView对象中。 When the window is loaded, the combobox gets values from my list of container classes and populates the combobox. 加载窗口后,组合框从我的容器类列表中获取值并填充组合框。 This works fine, but what I want is to be able to determine the listview index of the combobox whenever the SelectionChanged event is fired. 这工作正常,但我希望能在每次触发SelectionChanged事件时确定组合框的listview索引。

<ComboBox x:Name="TransFileOpt" ItemsSource="{Binding ComboBoxOptions}" SelectedIndex="{Binding SelectedIndex}" SelectionChanged="ComboBoxSelectionChanged"/>

When I've searched this before, all of the results are directed towards System.Windows.Forms.ListView while I'm working with System.Windows.Controls.ListView, so m_ListView.Rows[index] does not work. 当我之前进行搜索时,当我使用System.Windows.Controls.ListView时,所有结果都直接指向System.Windows.Forms.ListView,因此m_ListView.Rows[index]不起作用。

Basically I need a way to get the ListView index or item that contains the combobox that calls ComboBoxSelectionChanged (on firing of the SelectionChanged Event). 基本上,我需要一种获取ListView索引或包含调用ComboBoxSelectionChanged的组合框的项目的方法(在SelectionChanged事件触发时)。

The ListView block: ListView块:

    <ListView Name="m_ListView" ItemsSource="{Binding Tables}" Margin="0,28,0,132" SelectionMode="Extended" SelectedIndex="0" 
              RenderTransformOrigin="0.503,0.468" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
              SelectionChanged="ListView_SelectionChanged">
        <ListView.View>
            <GridView x:Name="m_TableGridView" AllowsColumnReorder="False"  >
                <GridViewColumn Width="50" Header="Loaded">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding Loaded}"></CheckBox>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Width="150" Header="Master File" DisplayMemberBinding ="{Binding MasterFileName}"/>
                <GridViewColumn Width="200" Header ="Translation File" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox x:Name="TransFileOpt" ItemsSource="{Binding ComboBoxOptions}" 
                                      SelectedIndex="{Binding SelectedIndex}" SelectionChanged="ComboBoxSelectionChanged"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
        <ListView.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Text }"></Setter>
            </Style>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.Resources>
    </ListView>

Other info: the project was generated through Visual Studio's wpf editor which defaulted to System.Windows.Controls for classes. 其他信息:该项目是通过Visual Studio的wpf编辑器生成的,该类默认为System.Windows.Controls。

Here is a working example for you: 这是一个适合您的工作示例:

<Window x:Class="ListViewGridView.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ListView Margin="10" Name="lvUsers" ItemsSource="{Binding  items}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Items" Width="150">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding containerItems}" SelectionChanged="ComboBox_SelectionChanged">
                                <ComboBox.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding}"/>
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

Codebehind: 代码隐藏:

public partial class MainWindow : Window
{
    public ObservableCollection<ItemContainer> items { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        items = new ObservableCollection<ItemContainer>();

        items.Add(new ItemContainer() { containerItems = new List<string>() { "Item1", "Item2", "Item3" } });
        items.Add(new ItemContainer() { containerItems = new List<string>() { "Item4", "Item5", "Item6" } });

        this.DataContext = this;
    }


    public static T FindAncestorOrSelf<T>(DependencyObject obj)
        where T : DependencyObject
    {
        while (obj != null)
        {
            T objTest = obj as T;

            if (objTest != null)
                return objTest;

            obj = VisualTreeHelper.GetParent(obj);
        }
        return null;
    }

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ListViewItem lvItem = FindAncestorOrSelf<ListViewItem>(sender as ComboBox);
        ListView listView = ItemsControl.ItemsControlFromItemContainer(lvItem) as ListView;
        int index = listView.ItemContainerGenerator.IndexFromContainer(lvItem);
        Console.WriteLine(index.ToString());
    }
}

And the simple model class: 和简单的模型类:

public class ItemContainer
{
    public List<string> containerItems { get; set; }
}

That's about it. 就是这样 Have fun! 玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM