C# 以编程方式选择ListView的某个项目时,展开该项目组
以下是我的wpf listview的代码。在这里,我以编程方式创建listitems,并按ItemFamily对它们进行分组,然后当我单击搜索按钮时,名为car的项目被选中。但包含项目“car”的ItemGroup仍然处于崩溃状态。我希望在以编程方式选择子项时自动展开此项组C# 以编程方式选择ListView的某个项目时,展开该项目组,c#,.net,wpf,listview,C#,.net,Wpf,Listview,以下是我的wpf listview的代码。在这里,我以编程方式创建listitems,并按ItemFamily对它们进行分组,然后当我单击搜索按钮时,名为car的项目被选中。但包含项目“car”的ItemGroup仍然处于崩溃状态。我希望在以编程方式选择子项时自动展开此项组 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; usin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<User> items = new List<User>();
items.Add(new User() { Name = "Dhal", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Food" });
items.Add(new User() { Name = "Dhal", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Food" });
items.Add(new User() { Name = "Dhal", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Food" });
items.Add(new User() { Name = "Dhal", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Food" });
items.Add(new User() { Name = "Car", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Toys" });
items.Add(new User() { Name = "Bus", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Toys" });
items.Add(new User() { Name = "Water Gun", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Toys" });
items.Add(new User() { Name = "Rubics Cube", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Toys" });
items.Add(new User() { Name = "Sun & Fun", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Helth Care" });
items.Add(new User() { Name = "Face Wash", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Helth Care" });
items.Add(new User() { Name = "LED TV 15\"", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Electronics" });
items.Add(new User() { Name = "Sub woofer", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Electronics" });
items.Add(new User() { Name = "Lava Lamp", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Electronics" });
items.Add(new User() { Name = "Play Station Portable", Price = "42", Tax = "A", Barcode = "12345", ItemFamily = "Electronics" });
lvUsers.ItemsSource = items;
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource);
PropertyGroupDescription groupDescription = new PropertyGroupDescription("ItemFamily");
view.GroupDescriptions.Add(groupDescription);
KeyboardNavigation.SetTabNavigation(lvUsers, KeyboardNavigationMode.Cycle);
lvUsers.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("ItemFamily", System.ComponentModel.ListSortDirection.Ascending));
lvUsers.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription("Name", System.ComponentModel.ListSortDirection.Ascending));
}
public class User
{
public string Name { get; set; }
public string Price { get; set; }
public string Tax { get; set; }
public string ItemFamily { get; set; }
public string Barcode { get; set; }
public int index { get; set; }
}
private int indexed_items;
private bool find(string str)
{
int i = -1;
foreach (User item in lvUsers.Items)
{
i++;
if (item.Name.Equals(str))
{
indexed_items = i;
return true;
}
}
return false;
}
private void select(string str)
{
List<User> items1 = new List<User>();
foreach (User item in lvUsers.Items)
{
if (item.Name.Equals(str))
{
lvUsers.SelectedIndex = indexed_items;
lvUsers.ScrollIntoView(item);
}
items1.Add(item);
}
}
private void btnsearch_Click(object sender, RoutedEventArgs e)
{
if (find("Car"))
{
select("Car");
}
else
{
MessageBox.Show("Not found");
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication4
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
列表项=新列表();
添加(新用户(){Name=“Dhal”、Price=“42”、Tax=“A”、Barcode=“12345”、itemlafmily=“Food”});
添加(新用户(){Name=“Dhal”、Price=“42”、Tax=“A”、Barcode=“12345”、itemlafmily=“Food”});
添加(新用户(){Name=“Dhal”、Price=“42”、Tax=“A”、Barcode=“12345”、itemlafmily=“Food”});
添加(新用户(){Name=“Dhal”、Price=“42”、Tax=“A”、Barcode=“12345”、itemlafmily=“Food”});
添加(新用户(){Name=“Car”、Price=“42”、Tax=“A”、Barcode=“12345”、itemlafmily=“Toys”});
添加(新用户(){Name=“Bus”、Price=“42”、Tax=“A”、Barcode=“12345”、ItemFamily=“Toys”});
添加(新用户(){Name=“Water Gun”、Price=“42”、Tax=“A”、Barcode=“12345”、itemlafmily=“Toys”});
添加(新用户(){Name=“Rubics-Cube”、Price=“42”、Tax=“A”、Barcode=“12345”、itemlafmily=“Toys”});
添加(新用户(){Name=“Sun&Fun”、Price=“42”、Tax=“A”、Barcode=“12345”、itemlafmily=“Helth Care”});
添加(新用户(){Name=“Face Wash”、Price=“42”、Tax=“A”、Barcode=“12345”、ItemFamily=“Helth Care”});
添加(新用户(){Name=“LED TV 15\”、Price=“42”、Tax=“A”、Barcode=“12345”、itemlafmily=“Electronics”});
添加(新用户(){Name=“Sub-woofer”、Price=“42”、Tax=“A”、Barcode=“12345”、itemlafmily=“Electronics”});
添加(新用户(){Name=“Lava Lamp”、Price=“42”、Tax=“A”、Barcode=“12345”、itemlafmily=“Electronics”});
添加(新用户(){Name=“Play Station Portable”、Price=“42”、Tax=“A”、Barcode=“12345”、ItemFamily=“Electronics”});
lvUsers.ItemsSource=项目;
CollectionView视图=(CollectionView)CollectionViewSource.GetDefaultView(lvUsers.ItemsSource);
PropertyGroupDescription groupDescription=新的PropertyGroupDescription(“ItemFamily”);
view.groupDescription.Add(groupDescription);
KeyboardNavigation.SetTabNavigation(lvUsers,KeyboardNavigationMode.Cycle);
lvUsers.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription(“ItemFamily”,System.ComponentModel.ListSortDirection.升序));
lvUsers.Items.SortDescriptions.Add(new System.ComponentModel.SortDescription(“Name”,System.ComponentModel.ListSortDirection.升序));
}
公共类用户
{
公共字符串名称{get;set;}
公共字符串Price{get;set;}
公共字符串Tax{get;set;}
公共字符串ItemFamily{get;set;}
公共字符串条形码{get;set;}
公共int索引{get;set;}
}
私有int索引_项;
私有布尔查找(字符串str)
{
int i=-1;
foreach(lvUsers.Items中的用户项)
{
i++;
if(item.Name.Equals(str))
{
索引项=i;
返回true;
}
}
返回false;
}
私有void选择(字符串str)
{
列表项1=新列表();
foreach(lvUsers.Items中的用户项)
{
if(item.Name.Equals(str))
{
lvUsers.SelectedIndex=索引项;
lvUsers.ScrollIntoView(项目);
}
项目1.添加(项目);
}
}
私有无效BTN搜索单击(对象发送者,路由目标e)
{
如果(查找(“汽车”))
{
选择(“汽车”);
}
其他的
{
MessageBox.Show(“未找到”);
}
}
}
}
<Window x:Class="WpfApplication4.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 removed="Aqua">
<ListView Name="lvUsers" removed="Transparent" BorderThickness="0">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Height" Value="45" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Width="20" DisplayMemberBinding="{Binding space}" />
<GridViewColumn Header="Control" Width="410" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Name}" Width="400" Foreground="Black" Height="40" Margin="0" FontSize="20" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Control" Width="210" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Price}" Width="200" Foreground="Black" Height="40" Margin="0" FontSize="20" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Name="Expander">
<Expander.Header>
<StackPanel Name="expnder_stackpanel" Orientation="Horizontal" Height="75">
<Canvas>
<TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="White" FontSize="22" VerticalAlignment="Bottom" Canvas.Top="20" Canvas.Left="50" />
<TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="White" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" Canvas.Left="500" VerticalAlignment="Bottom" Canvas.Top="20" />
<TextBlock Text=" item(s)" FontSize="22" Foreground="Silver" FontStyle="Italic" VerticalAlignment="Bottom" Canvas.Top="20" Canvas.Left="400" />
</Canvas>
</StackPanel>
</Expander.Header>
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
<Button Content="Search" Width="200" Height="50" Name="btnsearch" Click="btnsearch_Click" />
</Grid>
</Window>