Link to home
Start Free TrialLog in
Avatar of Ashok
AshokFlag for United States of America

asked on

How to change Caption of Header at Runtime?

I am using Silverlight 3.0.

<data:DataGrid Name="dglaborProj" AutoGenerateColumns="False">
<data:DataGrid.Columns>
    <data:DataGridTextColumn Width="Auto" IsReadOnly="True">
        <data:DataGridTextColumn.HeaderStyle>
            <Style TargetType="dataPrimitives:DataGridColumnHeader">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text=" "/>
                                <TextBlock Name="txtCol1" Text="Last Year" TextAlignment="Center"/>
                                <TextBlock Text=" "/>
                                <TextBlock Name="txtCol2" Text="12/28/2009" TextAlignment="Center"/>
                            </StackPanel>
                        </DataTemplate>
                  </Setter.Value>
             </Setter>
         </Style>
        </data:DataGridTextColumn.HeaderStyle>
    </data:DataGridTextColumn>
</data:DataGrid.Columns>
</data:DataGrid>
How can I change Caption set by txtCol2 (which is "12/28/2009") in the code at runtime?

Thanks,
Ashok
Avatar of DanSo1
DanSo1
Flag of Poland image

You can consider MVVM. In that case you can bind your TextBlock to the property in ViewModel. Any changes in that property will be visible in your view with TextBlock
Avatar of Ashok

ASKER

I am not sure how to do this.  Do you have any link or sample code?

Thanks,
Ashok
ASKER CERTIFIED SOLUTION
Avatar of DanSo1
DanSo1
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ashok

ASKER

DanSo1,

I am going to try this tomorrow.

Thanks,
Ashok
Avatar of Ashok

ASKER

DanSo1,

MVVM works, but it does not work when I want to change multiple Title in the header as in following.....

      DataGridTextColumn col2 = new DataGridTextColumn();
      col2.Width = new DataGridLength(92);
      col2.IsReadOnly = true;
      StringBuilder Cell_Sty2 = new StringBuilder();
      Cell_Sty2.Append("<Style ");
      Cell_Sty2.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
      Cell_Sty2.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
      Cell_Sty2.Append("xmlns:data='clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data' ");
      Cell_Sty2.Append("xmlns:dataPrimitives='clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data' ");
      Cell_Sty2.Append("TargetType='dataPrimitives:DataGridColumnHeader'>");
      Cell_Sty2.Append("<Setter Property='HorizontalContentAlignment' Value='Center'/>");
      Cell_Sty2.Append("<Setter Property='ContentTemplate'>");
      Cell_Sty2.Append("<Setter.Value>");
      Cell_Sty2.Append("<DataTemplate>");
      Cell_Sty2.Append("<StackPanel>");
      Cell_Sty2.Append("<TextBlock Text='{Binding FirstCol}' TextAlignment='Center'/>");  // <<== This binding does not work
      //Cell_Sty2.Append("<TextBlock Text='2008 Wk' TextAlignment='Center'/>");
      Cell_Sty2.Append("<TextBlock Text='Ending' TextAlignment='Center'/>");
      Cell_Sty2.Append("<TextBlock Text='Avg' TextAlignment='Center'/>");
      Cell_Sty2.Append("</StackPanel>");
      Cell_Sty2.Append("</DataTemplate>");
      Cell_Sty2.Append("</Setter.Value>");
      Cell_Sty2.Append("</Setter>");
      Cell_Sty2.Append("</Style>");
      col2.HeaderStyle = (Style)XamlReader.Load(Cell_Sty2.ToString());

If I have
<TextBox Name="txtTest" Text="{Binding Path=FirstCol, Mode=TwoWay}" Width="100" Grid.Row="1" Grid.Column="4"/>
in my XAML file, MVVM works.  I tested this and changed value at runtime and text in txtTest automatically changed when I used
((ViewModel)(DataContext)).FirstCol = "Hello";

Thanks,
Ashok
Well, you can easy check your bindings problems. Just run application in debug mode. Go to place just before your DataGrid will be shown, clear output in VS, fire your view and then you will see every bug in VS output window.