如何在WPF中获取单击列值或Listview的ID? [英] How to get the clicked column value or Id of Listview in WPF?

查看:118
本文介绍了如何在WPF中获取单击列值或Listview的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的应用程序中,我们有一个列表视图,并且数据正在通过XML文件加载.

我们有8列和3列以及一个复选框和标头.

当我选中带有标题的复选框时,我必须获取所检查的列的值或列的编号.

如何获取要获取的ID或列号我点击了吗?

我认为可能是通过循环实现的,但我不知道如何编写代码来获取它.


请提供一些代码.

Hi,

In my application, we have a listview and data is loading thru XML file.

We have 8 column and 3 column with a checkbox and header.

When i checked the checkbox with the header i have to get the column value or no of the column which i have checked.

How to get the ID or column number which i have clicked?

I think its possible thru looping but i dont how to write the code to get it.


Please provide some code.

推荐答案

选项之一,将ListView列内部的CheckBox('Tag)绑定到其GrdViewColumn标头.然后在CheckBox中单击回调,在树上行走以找到GridViewRowPresenter并通过比较column.Header和checkbox.Tag来遍历各列以获取正确的列索引.请参见下面的示例:


Xml:文件名为People.xml
<?xml version ="1.0" encoding ="utf-8" ?>
<人>
,<人名="Tom" LikeCar ="true"年龄="10"岁LikeCake ="true"/< Person Name ="Jen" LikeCar ="false"年龄="20"岁LikeCake ="false"/< Person Name ="Peter" LikeCar ="true"年龄="30"岁LikeCake =假"/<人名=李" LikeCar ="false"年龄="40"岁LikeCake ="true"/>
</People>


标记:
< Window x:Class ="ListViewTest.MainWindow"
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="" http://schemas.microsoft.com/winfx/2006/xaml "
标题="MainWindow";高度="350".宽度="525">
< Window.Resources>
< XmlDataProvider x:Key ="People"来源="People.xml"; XPath ="/People" />
</Window.Resources>
< StackPanel DataContext ="{StaticResource People}">
ItemsSource ="{Binding XPath = Person}">
DisplayMemberBinding =" {Binding XPath = @ Name }" />
< GridViewColumn x:Name ='likeCarColumn';标头="LikeCar" >
< GridViewColumn.CellTemplate>
<&的DataTemplate GT;
<复选框=器isChecked" {结合的XPath = @ LikeCar }" Tag ="{Binding ElementName = likeCarColumn,Path = Header}"点击="CheckBox_Click" />
</DataTemplate中>
</GridViewColumn.CellTemplate>
</GridViewColumn>
< GridViewColumn部首= QUOT;年龄" DisplayMemberBinding =" {Binding XPath = @ Age }" />
< GridViewColumn x:Name ="likeCakeColumn";标头="LikeCake" >
< GridViewColumn.CellTemplate>
<&的DataTemplate GT;
<复选框=器isChecked" {结合的XPath = @ LikeCake }" Tag =" {{Binding ElementName = likeCakeColumn,Path = Header}"点击="CheckBox_Click" />
</DataTemplate中>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView的>
</ListView中.查看>
</ListView>
</StackPanel>
</Window>


代码:
使用系统;
使用System.Diagnostics;
使用System.Windows;
使用System.Windows.Controls;

One of options, bind CheckBox('Tag) that is inside of ListView column to its GrdViewColumn Header. Then in the CheckBox click callback, walk the tree to find GridViewRowPresenter and iterate through columns to get the right column index by comparing column.Header and checkbox.Tag. Please see example below:


Xml: file name is People.xml
<?xml version="1.0" encoding="utf-8" ?>
<People>
  <Person Name="Tom" LikeCar="true" Age="10" LikeCake="true"/>
  <Person Name="Jen" LikeCar="false" Age="20" LikeCake="false"/>
  <Person Name="Peter" LikeCar="true" Age="30" LikeCake="false"/>
  <Person Name="Lee" LikeCar="false" Age="40" LikeCake="true"/>
</People>


Markup:
<Window x:Class="ListViewTest.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">
    <Window.Resources>
        <XmlDataProvider x:Key="People" Source="People.xml" XPath="/People" />
    </Window.Resources>
    <StackPanel DataContext="{StaticResource People}">
        <ListView Name="listview" ItemsSource="{Binding XPath=Person}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding XPath=@Name}" />
                    <GridViewColumn x:Name="likeCarColumn" Header="LikeCar" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding XPath=@LikeCar}" Tag="{Binding ElementName=likeCarColumn, Path=Header}" Click="CheckBox_Click" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Age" DisplayMemberBinding="{Binding XPath=@Age}" />
                    <GridViewColumn x:Name="likeCakeColumn" Header="LikeCake" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding XPath=@LikeCake}" Tag="{Binding ElementName=likeCakeColumn, Path=Header}" Click="CheckBox_Click" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </StackPanel>
</Window>


Code:
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

命名空间ListViewTest
{
////< summary>
///MainWindow.xaml的交互逻辑
///</summary>
公共部分类MainWindow:Window
{
public MainWindow()
{
InitializeComponent();
}

namespace ListViewTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

私有void CheckBox_Click(对象发送方,RoutedEventArgs e)
{
=(GridViewRowPresenter)cp.Parent;

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            ContentPresenter cp = (ContentPresenter)cb.TemplatedParent;
            GridViewRowPresenter gvrp = (GridViewRowPresenter)cp.Parent;

inte index = -1;
foreach(gvrp.Columns中的GridViewColumn列)
{{ >> index = gvrp.Columns.IndexOf(column);
}
}

            int index = -1;
            foreach (GridViewColumn column in gvrp.Columns)
            {
                if (column.Header == cb.Tag)
                {
                    index = gvrp.Columns.IndexOf(column);
                }
            }

如果(index!= -1)
{{br/>)Trace.WriteLine(gvrp.Columns [index]); >抛出新的ArgumentException(``找不到列索引.'');
}
}
}
}

            if (index != -1)
            {
                Trace.WriteLine(gvrp.Columns[index]);
            }
            else
            {
                throw new ArgumentException("Could not find index of column.");
            }
        }
    }
}


这篇关于如何在WPF中获取单击列值或Listview的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆