tags:

views:

7299

answers:

6
 <my:DataGridTemplateColumn CanUserResize="False" Width="150" Header="{Binding MeetingName, Source={StaticResource LocStrings}}" SortMemberPath="MeetingName    > </my:DataGridTemplateColumn>");

I have the above column in a Silverlight grid control. But it is giving me a XamlParser error because of how I am trying to set the Header property. Has anyone done this before? I want to do this for multiple languages.

Also my syntax for the binding to a resouce is correct because I tried it in a lable outside of the grid.

+9  A: 

You can't Bind to Header because it's not a FrameworkElement. You can make the text dynamic by modifying the Header Template like this:

xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"

<data:DataGridTemplateColumn>   
   <data:DataGridTemplateColumn.HeaderStyle>
       <Style TargetType="dataprimitives:DataGridColumnHeader">
          <Setter Property="Template">
             <Setter.Value>
                <ControlTemplate>                                        
                  <TextBlock Text="{Binding MeetingName, Source={StaticResource LocStrings}}" />                
               </ControlTemplate>
            </Setter.Value>
         </Setter>
      </Style>
   </data:DataGridTemplateColumn.HeaderStyle>
</data:DataGridTemplateColumn>
Adam Kinney
Great answer and thanks for the code sample
Kyle
How do you keep the visual styling from the original header?
Edward Brey
Thanks for the answer. You helped alot.
Rob Vermeulen
A: 

That is not dynamic tho, the staticresource is static, is there any way to do this dynamic? thst answer does not answer the question imo.

Peter
+1  A: 

Why not simply set this in code:

dg1.Columns[3].Header = SomeDynamicValue;
Jersey Dude
A: 

I got some solution for the binding. Since you use DataGridTemlateColumn, subclass it and add a property of type Binding named for instance "HeaderBinding". Now you can bind to that property from the XAML. Next, you should propagate the binding to the TextBlock in the DataTemplate of your header. For instance, you can do it with OnLoaded event of that TextBlock.

 HeaderTextBlock.SetBinding(TextBlock.TextProperty, HeaderBinding);

That's it. If you have more columns and want to use only one DataTemplate then it's a bit more complicated, but the idea is the same.

Dimitar
A: 

To keep the visual styling from the original header, use ContentTemplate instead of Template:

<Setter Property="ContentTemplate">
<Setter.Value>
    <DataTemplate>
        <Image Source="<image url goes here>"/>
    </DataTemplate>
</Setter.Value>

Lars Holm Jensen
Can't seem to get binding to work in the DataTemplate on a TextBlock: <TextBlock Text="{Binding HeaderText}"/> replaces the Image in your example.
ptoinson
A: 

We need to explicitly set the SortMemberPath to enable the sorting on template column.

below link provides good explanation of sorting in datagrid with template columns.

http://www.a2zmenu.com/Silverlight/Datagrid-TemplateColumn-Sorting-in-Silverlight-3.aspx

rs.emenu