Solved

Retrieve selected value from templated datagrid comboboxcolumn in WPF

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

13 Experts available now in Live!

Get 1:1 Help Now