Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to bind a DataGridTemplateColumn to custom Observable Collection like following:

XML
<WPFToolkit:DataGridTemplateColumn Header="Party Name" Width="150">                            <WPFToolkit:DataGridTemplateColumn.CellTemplate>
 <DataTemplate>
  <TextBlock Text="{Binding PartyId}"></TextBlock>
                                </DataTemplate>
                           </WPFToolkit:DataGridTemplateColumn.CellTemplate>
                            <WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
  <DataTemplate>
     <ComboBox SelectedItem="{Binding Path=PartyId}" ItemsSource="{Binding Path = PartyCollection}" ></ComboBox>
  </DataTemplate>
                            </WPFToolkit:DataGridTemplateColumn.CellEditingTemplate>
   </WPFToolkit:DataGridTemplateColumn>


where PartyCollection is property of my same class
XML
public ObservableCollection<Party> PartyCollection
        {
            get
            {
                if (_collectionParties == null)
                {
                    return new ObservableCollection<Party>();
                }
                return _collectionParties;
            }
        }


_collectionParties is filled properly and my Party class contains properties like PartyId, PartyName and so on. The issue is it cannot bind with the column so Combobox is not getting filled. I have searched a lot for a solution but most examples I found bind to the static resource.

Any help is greatly appreciated

Thanks in advance


Update:

1. Which object the DataGrid is binded to?
2. Is PartyCollection is a member of the object which you binded with the DataGrid?
Give answer to my questions and I will see what I can do for you.
- Venkatesh Mookkan


1. DataGrid is bind to Observation generic with class Order at runtime 2. No
if any more question please let me know
- Khaniya
Posted
Updated 5-Jan-11 22:08pm
v4
Comments
Venkatesh Mookkan 5-Jan-11 21:58pm    
1. Which object the DataGrid is binded to?
2. Is PartyCollection is a member of the object which you binded with the DataGrid?

Give answer to my questions and I will see what I can do for you.
Khaniya 6-Jan-11 1:59am    
1. DataGrid is bind to Observation generic with class Order at runtime
2. No

if any more question please let me know

1 solution

XML
<ComboBox SelectedItem="{Binding Path=PartyId}" ItemsSource="{Binding Path = PartyCollection}" ></ComboBox>


This is will not as you haven't specified the Source for the ItemsSource. You can ignore Source only if the DataGrid's ItemsSource (i.e. Collection of Order) has PartyCollection as member.

I suggestion you to create a Property PartyCollection in the UserControl or Window where you placed the DataGrid

Then bind like,

XML
<ComboBox SelectedItem="{Binding Path=PartyId}" ItemsSource="{Binding Path = PartyCollection, RelativeSource={RelativeSource AncestorType={x:Type loc:YourControlName}}}" ></ComboBox>


Here "loc" is the namespace of the UserControl or Window. "YourControlName" is the class name.

Happy coding.

Mark it as answer if it helps you.
 
Share this answer
 
Comments
Khaniya 6-Jan-11 7:00am    
Thanks I got it
Venkatesh Mookkan 6-Jan-11 7:01am    
You are most welcome. And thanks are voting my answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900