Fallowing are the codes for my wpf listview.there I'm creating listitems programaticaly and group them by ItemFamily then when I click the search button item named car is selected. but ItemGroup which contains the item "car" is still collapsed.I want this itemgroup to automaticaly expanded when it's child item is selected programmaticaly.
<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>
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
{
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");
}
}
}
}