Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Community,

I've set a own Class-Object to my DataContext.

this Object contains a Property called "MergedGerman", Type: System.Data.DataTable.

In each Cell of this DataTable, there is no simple string, but a own created class-object called "Text".

This Text-Class contains a "Caption" property, Type: System.String

C#
public class Text: DependencyObject
{
   public static readonly DependencyProperty CaptionProperty =
   DependencyProperty.Register("Caption", typeof(string), typeof(Text));


   public string Caption
   {
      get { return (string)GetValue(CaptionProperty); }
      set { SetValue(CaptionProperty, value); }
   }

   // other Properties
   // ....
}


I've bound a dataGrids ItemSource to the "MergedGerman"-Property of my DataContext

XML
<DataGrid x:Name="Dgrid_Original" Margin="0,5,0,0" Width="198"  AutoGeneratingColumn="Dgrid_Original_AutoGeneratingColumn"  ItemsSource ="{Binding Path=MergedGerman}">
</DataGrid>



My DataGrid displays the Text-Object by using the ToString()-Method.

Screenshot[]

But I want to display the Caption-Property of my Text-Object.

How works it ?


I've tried the following:

1.)


XML
<DataGrid x:Name="Dgrid_Original" Margin="0,5,0,0" Width="198"  AutoGeneratingColumn="Dgrid_Original_AutoGeneratingColumn"  ItemsSource ="{Binding Path=MergedGerman}">

<DataGrid.Columns>
   <DataGridTextColumn Header="Test" Binding="{Binding Caption}"/>
</DataGrid.Columns>
</DataGrid>


2.)

XML
<DataGrid x:Name=quot;Dgrid_Original" Margin=&
quot;0,5,0,0" Width="198"  AutoGeneratingColumn="Dgrid_Original_AutoGeneratingColumn"  ItemsSource ="{Binding Path=MergedGerman}">

<DataGrid.Columns>
   <DataGridTemplateColumn Header="Test">
      <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <TextBox Text="{Binding Caption}"/>
      </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
   </DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>


The result of 1.) and of 2.) is a empty textbox :-(

Do you have an idea ?
Posted
Comments
Mark Salsbery 19-Jul-11 18:58pm    
Shouldn't the binding path be Text.Caption and not just Caption?
Jeff.Jefferson 20-Jul-11 1:56am    
it doesn't work :( just tried...

1 solution

Yeah :)

It's sooo easy !

XML
<DataGrid.Columns>
   <DataGridTextColumn Header="Test" Binding="{Binding NameOfTheColumnOfTheItemSourceWhichHaveToBindToThisDataGridTextColumn.Caption}"/>
</DataGrid.Columns>
 
Share this 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