Solved

Retrieve selected value from templated datagrid comboboxcolumn in WPF

Posted on 2014-09-11
5
1,797 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 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