Solved

Retrieve selected value from templated datagrid comboboxcolumn in WPF

Posted on 2014-09-11
5
1,844 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
[X]
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
  • 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

751 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