[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to change Caption of Header at Runtime?

Posted on 2010-01-05
6
Medium Priority
?
1,230 Views
Last Modified: 2013-12-17
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
0
Comment
Question by:Ashok
  • 3
  • 3
6 Comments
 
LVL 7

Expert Comment

by:DanSo1
ID: 26198471
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
0
 
LVL 13

Author Comment

by:Ashok
ID: 26199235
I am not sure how to do this.  Do you have any link or sample code?

Thanks,
Ashok
0
 
LVL 7

Accepted Solution

by:
DanSo1 earned 2000 total points
ID: 26199327
OK. Let's do first steps:
Binding:
<TextBlock Text="{Binding Path=Email}" />

Property:
In ViewModel you need to create property: see code below

A special link for you:  
http://dl.infragistics.com/pixel8/media/Screencasts/mvvm/wpf/Full/pixel8-mvvm-wpf.wmv 




public string Email
        {
            get { return _customer.Email; }
            set
            {
                if (value == _customer.Email)
                    return;

                _customer.Email = value;

                base.OnPropertyChanged("Email");
            }
        }

Open in new window

0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 13

Author Comment

by:Ashok
ID: 26206267
DanSo1,

I am going to try this tomorrow.

Thanks,
Ashok
0
 
LVL 13

Author Comment

by:Ashok
ID: 26300437
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
0
 
LVL 7

Expert Comment

by:DanSo1
ID: 26301067
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.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

825 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