Get selectedvalue of the combobox embedded in GridView

Nov 28, 2008 at 1:12 PM
Hi,

 I have a listview having view = GridView. There is a combobox added to the grid using FrameworkElementFactory. Values are added using the CellTemplate. 
On the listview itemselected change event  how can we get the selected value of the combobox?

I tried using VisualHelper class , ContentPresenter and DataTemplate's FindName method. But could not get the required result. Please refer the code below.


Thank You.

Hiral

 

using

 

System;

 

using

 

System.Windows;

 

using

 

Microsoft.Practices.CompositeUI;

 

using

 

Microsoft.Practices.CompositeUI.SmartParts;

 

using

 

Microsoft.Practices.CompositeUI.WPF;

 

using

 

Microsoft.Practices.ObjectBuilder;

 

using

 

System.Windows.Media;

 

using

 

System.Windows.Controls;

 

using

 

TreeListViewSample.Infrastructure.Interface;

 

using

 

System.Windows.Data;

 

namespace

 

TreeListViewSample.TreeListViewModule

 

{

 

/// <summary>

 

 

/// Interaction logic for TreeListView.xaml

 

 

/// </summary>

 

 

public partial class TreeListView : System.Windows.Controls.UserControl, ITreeListView, IDisposable

 

{

 

/// <summary>

 

 

/// Initialize a new instance of <see cref="TreeListView"/>.

 

 

/// <summary>

 

 

DataTemplate template;

 

 

FrameworkElementFactory factoryComboBox;

 

 

GridView myGridView;

 

 

public TreeListView()

 

{

InitializeComponent();

 

this.Loaded += new RoutedEventHandler(OnLoad);

 

}

 

/// <summary>

 

 

/// Called when WPF has loaded the control and is ready to display it.

 

 

/// <summary>

 

 

/// <param name="sender">Source of the event</param>

 

 

/// <param name="e">Extra information about the event</param>

 

 

public void OnLoad(object sender, RoutedEventArgs e)

 

{

_presenter.OnViewReady();

 

 

BindGrid();

 

 

 

}

 

private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)

 

{

 

int idx=listView1.Items.IndexOf(listView1.SelectedItem);

 

 

 

 

ComboBox currentCellTemplateGridContainer = (ComboBox)ListViewHelper.GetElementFromCellTemplate(listView1, 3, 1, "ResponseAndRating"); /* Where

 

 

 

 

 

public static class ListViewHelper

 

{

 

 

public static FrameworkElement GetElementFromCellTemplate(ListView listView, Int32 column, Int32 row, String name)

 

{

 

if (row >= listView.Items.Count || row < 0)

 

{

 

throw new ArgumentOutOfRangeException("row");

 

}

 

GridView gridView = listView.View as GridView;

 

 

if (gridView == null)

 

{

 

return null;

 

}

 

if (column >= gridView.Columns.Count || column < 0)

 

{

 

throw new ArgumentOutOfRangeException("column");

 

}

 

 

ListViewItem item = listView.ItemContainerGenerator.ContainerFromItem(listView.Items[row]) as ListViewItem;

 

 

 

if (item != null)

 

{

 

if (!item.IsLoaded)

 

{

item.ApplyTemplate();

}

 

GridViewRowPresenter rowPresenter = GetFrameworkElementByName<GridViewRowPresenter>(item);

 

 

 

if (rowPresenter != null)

 

{

rowPresenter.ApplyTemplate();

 

 

 

ContentPresenter templatedParent = VisualTreeHelper.GetChild(rowPresenter, column) as ContentPresenter;

 

 

 

DataTemplate dataTemplate =gridView.Columns[column].CellTemplate;

 

 

if (dataTemplate != null && templatedParent != null)

 

{

templatedParent.ApplyTemplate();

 

return dataTemplate.FindName(name, templatedParent) as FrameworkElement;

 

}

}

}

 

 

return null;

 

 

}

 

 

private static T GetFrameworkElementByName<T>(FrameworkElement referenceElement) where T : FrameworkElement

 

{

FrameworkElement child = null;

 

 

for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceElement); i++)

 

{ child =

VisualTreeHelper.GetChild(referenceElement, i) as FrameworkElement;

 

System.Diagnostics.

Debug.WriteLine(child);

 

 

if (child != null && child.GetType() == typeof(T)) { break; }

 

 

else if (child != null) { child = GetFrameworkElementByName<T>(child);

 

 

if (child != null && child.GetType() == typeof(T)) { break; } } } return child as T; } }

 

 

 

private void BindGrid()

 

{

 

try

 

{

 

string[] arr1 = new string[10];

 

arr1[0] =

"FirstItem";

 

arr1[1] =

"SecondItem";

 

arr1[3] =

"ThirdItem";

 

 

 

string[] arr2 = new string[10];

 

arr2[0] =

"S2FirstItem";

 

arr2[1] =

"S2SecondItem";

 

arr2[3] =

"S2ThirdItem";

 

 

 

 

myGridView = new GridView();

 

myGridView.AllowsColumnReorder =

true;

 

myGridView.ColumnHeaderToolTip =

"Employee Information";

 

 

GridViewColumn gvc1 = new GridViewColumn();

 

gvc1.DisplayMemberBinding =

new Binding("FirstName");

 

gvc1.Header =

"FirstName";

 

gvc1.Width = 100;

myGridView.Columns.Add(gvc1);

 

GridViewColumn gvc2 = new GridViewColumn();

 

gvc2.DisplayMemberBinding =

new Binding("LastName");

 

gvc2.Header =

"Last Name";

 

gvc2.Width = 100;

myGridView.Columns.Add(gvc2);

 

GridViewColumn gvc3 = new GridViewColumn();

 

gvc3.DisplayMemberBinding =

new Binding("EmployeeNumber");

 

gvc3.Header =

"Employee No.";

 

gvc3.Width = 100;

myGridView.Columns.Add(gvc3);

 

GridViewColumn gvc4 = new GridViewColumn();

 

gvc4.Header =

"RatingAndResponse";

 

myGridView.Columns.Add(gvc4);

 

ComboBox cmb = new ComboBox();

 

cmb.Width = 60;

template =

new DataTemplate();

 

factoryComboBox =

new FrameworkElementFactory(typeof(ComboBox));

 

factoryComboBox.SetValue(

ComboBox.HorizontalAlignmentProperty, HorizontalAlignment.Right);

 

factoryComboBox.SetBinding(

ComboBox.ItemsSourceProperty, new Binding("ResponseAndRating"));

 

factoryComboBox.SetValue(

ComboBox.WidthProperty, cmb.Width);

 

template.VisualTree = factoryComboBox;

gvc4.CellTemplate = template;

 

System.Collections.ObjectModel.

Collection<Employee> emp = new System.Collections.ObjectModel.Collection<Employee>();

 

 

Employee employee = new Employee();

 

employee.FirstName =

"Hr";

 

employee.LastName =

"Dv";

 

employee.EmployeeNumber =

"1";

 

employee.ResponseAndRating = arr1;

emp.Add(employee);

 

Employee employee1 = new Employee();

 

employee1.FirstName =

"Bg";

 

employee1.LastName =

"Dv";

 

employee1.EmployeeNumber =

"2";

 

employee1.ResponseAndRating = arr2;

emp.Add(employee1);

 

 

listView1.ItemsSource = emp;

 

 

listView1.View = myGridView;

 

 

}

 

catch (Exception ex)

 

{

 

MessageBox.Show(ex.Message);

 

}

}

 

public class Employee

 

{

 

private string _firstName;

 

 

private string _lastName;

 

 

private string _empNumber;

 

 

private string[] _responseAndRating;

 

 

 

public string FirstName

 

{

get{return _firstName;} set{_firstName = value;} }

 

 

public string LastName

 

{

get { return _lastName; } set { _lastName=value;} }

 

 

public string EmployeeNumber

 

{

get { return _empNumber; } set { _empNumber=value;} }

 

 

public string[] ResponseAndRating

 

{

 

get { return _responseAndRating; }

 

 

set { _responseAndRating = value; }

 

}

}

}

Nov 28, 2008 at 2:44 PM
Hi,

You have posted on the wrong forum - this is the forum for WPF toolkit (i.e. DataGrid, Ribbon etc...). Try the main WPF forum instead:

http://social.msdn.microsoft.com/Forums/en/wpf/threads/

Colin E. --
http://wpfadventures.wordpress.com/ - my WPF blog
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx - WPF DataGrid Practical Examples


Aug 10, 2010 at 2:03 PM

Hi Vyashiral. 

                   To achieve this you have to override Equal and gethascode methods for that class whoes object you want to be selected in combobox e.g.

 

Class ABC

{

Property1

property2

property 3

...

 

public override bool Equals(object obj)

{

 

 

if (obj == null || GetType() != obj.GetType())

{

 

return false;

}

 

return (yourcondition here);

}

 

// override object.GetHashCode
 

 

public override int GetHashCode()

{

 

return this.property1.GetHashCode();
  // or you can send hash code of any property from your class

}

}

 

hope this will help.