Ashok
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.H eaderStyle >
<Style TargetType="dataPrimitives :DataGridC olumnHeade r">
<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. HeaderStyl e>
</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
<data:DataGrid Name="dglaborProj" AutoGenerateColumns="False
<data:DataGrid.Columns>
<data:DataGridTextColumn Width="Auto" IsReadOnly="True">
<data:DataGridTextColumn.H
<Style TargetType="dataPrimitives
<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.
</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
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
ASKER
I am not sure how to do this. Do you have any link or sample code?
Thanks,
Ashok
Thanks,
Ashok
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
DanSo1,
I am going to try this tomorrow.
Thanks,
Ashok
I am going to try this tomorrow.
Thanks,
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:da ta='clr-na mespace:Sy stem.Windo ws.Control s;assembly =System.Wi ndows.Cont rols.Data' ");
Cell_Sty2.Append("xmlns:da taPrimitiv es='clr-na mespace:Sy stem.Windo ws.Control s.Primitiv es;assembl y=System.W indows.Con trols.Data ' ");
Cell_Sty2.Append("TargetTy pe='dataPr imitives:D ataGridCol umnHeader' >");
Cell_Sty2.Append("<Setter Property='HorizontalConten tAlignment ' Value='Center'/>");
Cell_Sty2.Append("<Setter Property='ContentTemplate' >");
Cell_Sty2.Append("<Setter. Value>");
Cell_Sty2.Append("<DataTem plate>");
Cell_Sty2.Append("<StackPa nel>");
Cell_Sty2.Append("<TextBlo ck Text='{Binding FirstCol}' TextAlignment='Center'/>") ; // <<== This binding does not work
//Cell_Sty2.Append("<TextB lock Text='2008 Wk' TextAlignment='Center'/>") ;
Cell_Sty2.Append("<TextBlo ck Text='Ending' TextAlignment='Center'/>") ;
Cell_Sty2.Append("<TextBlo ck Text='Avg' TextAlignment='Center'/>") ;
Cell_Sty2.Append("</StackP anel>");
Cell_Sty2.Append("</DataTe mplate>");
Cell_Sty2.Append("</Setter .Value>");
Cell_Sty2.Append("</Setter >");
Cell_Sty2.Append("</Style> ");
col2.HeaderStyle = (Style)XamlReader.Load(Cel l_Sty2.ToS tring());
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
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=
Cell_Sty2.Append("xmlns:da
Cell_Sty2.Append("xmlns:da
Cell_Sty2.Append("TargetTy
Cell_Sty2.Append("<Setter Property='HorizontalConten
Cell_Sty2.Append("<Setter Property='ContentTemplate'
Cell_Sty2.Append("<Setter.
Cell_Sty2.Append("<DataTem
Cell_Sty2.Append("<StackPa
Cell_Sty2.Append("<TextBlo
//Cell_Sty2.Append("<TextB
Cell_Sty2.Append("<TextBlo
Cell_Sty2.Append("<TextBlo
Cell_Sty2.Append("</StackP
Cell_Sty2.Append("</DataTe
Cell_Sty2.Append("</Setter
Cell_Sty2.Append("</Setter
Cell_Sty2.Append("</Style>
col2.HeaderStyle = (Style)XamlReader.Load(Cel
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))
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.