Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Retrieve selected value from templated datagrid comboboxcolumn in WPF

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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