Solved

Retrieve selected value from templated datagrid comboboxcolumn in WPF

Posted on 2014-09-11
5
1,781 Views
Last Modified: 2014-09-20
Hi EE,
    I recently moved from winform to WPF, so bear with me if my question sounds a bit weird as I realised there's a bit learning curve trying to adopt the changes.

   anyways, I'm trying to use a datagrid control, which has a combobox column, I created template for the combobox to have both Image and person Name showing inside it (Binding works ok), however I found its impossible to get the combox's selected value, i.e. User selects one person, after a button click, I'd like to grab that person's name, how would I do that in WPF?

   Datagrid is binding to an object list of (PersonEvent), Combobox is binding to an object list of (Person), List of Person is a property of PersonEvent.

Xaml like this:
<DataGridTextColumn   Binding="{Binding EventNumber}" MinWidth="90" IsReadOnly="True" Header="Event Number"
                                              HeaderStyle ="{StaticResource DgvHeaderStyle}" ElementStyle="{StaticResource DgvTextCellStyle}" />

                        <DataGridTemplateColumn   Header="Person"  MinWidth="160" HeaderStyle="{StaticResource DgvHeaderStyle}" >
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox x:Name="cbPerson"  ItemsSource="{Binding Person}"  SelectedValuePath="PersonName"    >
                                        
                                        <ComboBox.ItemTemplate>
                                            <DataTemplate>
                                                <StackPanel Orientation="Horizontal">
                                                    <Image x:Name="imgPerson" Width="60"  Height="60" Stretch="Fill" Source="{Binding Photo, Converter={StaticResource nullImageConverter}}" />
                                                    <TextBlock x:Name="tbPerson" Text="{Binding PersonName}" Margin="5,0,0,0"/>
                                                </StackPanel>
                                            </DataTemplate>
                                        </ComboBox.ItemTemplate>
                                    </ComboBox>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

Open in new window


in vb file i set itemssource to datagrid:
Dim PersonEvent as new ObservableCollection(Of PE)
'Populate the list'
dgv.Itemssource = PersonEvent

Open in new window


Above displaying all working fine, but I'm struggling to get 'cbPerson' selected value as I cant reference this combobox in code, is there a solution to this ? or is there a better way of doing what I'm trying to do?
0
Comment
Question by:miketonny
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40323230
Are you talking about this ComboBox?

<ComboBox x:Name="cbPerson"  ItemsSource="{Binding Person}"  SelectedValuePath="PersonName"    >

What do you need to do with the selected value from "cbPerson"?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 40323918
You have couple of options

1) Handle the PreparingCellForEdit event of the grid to get a reference to the combobox using e.EditingElement

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.preparingcellforedit(v=vs.110).aspx


2) Try following code by replacing the correct column index

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
      Dim row As DataGridRow = TryCast(Me.dg.ItemContainerGenerator.ContainerFromIndex(Me.dg.SelectedIndex), DataGridRow)
      Dim ele As ComboBox = TryCast(Me.dg.Columns(0).GetCellContent(row), ComboBox)
      Console.WriteLine(ele.Text)
End Sub
0
 
LVL 2

Assisted Solution

by:miketonny
miketonny earned 0 total points
ID: 40324132
@bob learned,  
yes that was the one i needed to get its selected value, I need the value to put into a new object called 'Job assigned', basically has 'person' info and 'job' detail.

@CodeCruiser,
thanks for the comment, I did try second option a few days ago after i did a lot of research from google, combobox text showed nothing for some reason.


I found a working solution from google yesterday however, is to bind the selectedItem of 'cbPerson' to a new property 'Person' in 'Person's list, making

                    <ComboBox x:Name="cbPerson"  ItemsSource="{Binding Persons}"  SelectedItem="{Binding SelectedPerson, Mode=Twoway,  UpdateSourceTrigger=PropertyChanged}"    >
                

Open in new window


by doing this, whenever i change selection from person list, i trigger a property change of selectedperson, so all i have to do is read 'SelectedPerson' at the end
0
 
LVL 2

Author Closing Comment

by:miketonny
ID: 40334182
got mine as the working solution currently, however the other options are also viable but needs to be tried out
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now