Silverlight 4.0 : How to get DatagridTemplateColumn Header in code behind
have in XAML a datagridtemplate column
<sdk:DataGridTemplateColumn x:Name="Urgency">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Image Source="{Binding UrgencyUri}"
VerticalAlignment="Stretch"
Stretch="UniformToFill"
Height="10" Width="10" Margin="0,0,5,0"/>
<TextBlock Text="{Binding Urgency}"/>
</StackPanel>
</D开发者_StackOverflow中文版ataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.HeaderStyle>
<Style TargetType="primitive:DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Path=Urgency, Source={StaticResource MHVWindowResources}}" Grid.Column="0" HorizontalAlignment="Left" />
<filter:DataGridColumnFilter Grid.Column="1" HorizontalAlignment="Right" />
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</sdk:DataGridTemplateColumn.HeaderStyle>
</sdk:DataGridTemplateColumn>
I used this code below however it returns null
DataGridTemplateColumn templateColumn = dataGrid.Columns[columnIndex] as DataGridTemplateColumn;
if (templateColumn != null)
{
string header = templateColumn.Header as string;
if (header == null)
{
return null;
}
bindingPath = header;
}
Is their anything that I missed out? Thanks I want to know how to get the column header in code behind.
Add this line:
var header = templateColumn.Header
place a break point here debug and check what the type is in the quick watch.
My bet is the header is probably never a string so it always null when you use:
string header = templateColumn.Header as string;
hope that helps
精彩评论