Silverlight binding a HeaderTemplate
I have an accordion control, here's the xaml:
<UserControl x:Class="CasesPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="htt开发者_C百科p://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
mc:Ignorable="d"
d:DesignHeight="1050" d:DesignWidth="1600">
<UserControl.Resources>
<DataTemplate x:Key="AccordionItemHeaderTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding CaseName}"/>
</Grid>
</DataTemplate>
</UserControl.Resources>
<Border Margin="20,20,0,20" Background="White" BorderBrush="Transparent" BorderThickness="0" CornerRadius="10">
<toolkit:Accordion Margin="30" Name="CasesListAccordion" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ItemContainerStyle="{StaticResource AccordionContainerStyleLawBot}" BorderBrush="{x:Null}" SelectionMode="ZeroOrOne" SelectionSequence="CollapseBeforeExpand" Style="{StaticResource NewAccordionStyle}"
ItemsSource="{Binding}" AccordionButtonStyle="{StaticResource AccordionButtonStyleNotEdited}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Foreground="Black" Background ="White"
SelectedItemsChanged="CasesListAccordion_SelectedItemsChanged">
</toolkit:Accordion>
</Border>
In my User control resources, I also have a data template which I want to use for headertemplate in each accordion item.
The accordion items are populated from the code, I do this because I receive them dynamically.
Here's the code:
foreach(ECase Case in Cases)
{
//Create an accordion item
AccordionItem item = new AccordionItem();
item.Tag = Case;
item.DataContext = Case;
item.HeaderTemplate = (DataTemplate)this.Resources["AccordionItemHeaderTemplate"];
}
The class ECase has a Member called CaseName . I bind this member in the xaml in the datatemplate to the textblock:
<DataTemplate x:Key="AccordionItemHeaderTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding CaseName}"/>
</Grid>
</DataTemplate>
And I also assign the DataContext of the accordionItem to the ECase:
item.DataContext = Case;
And still the CaseName is not displayed in the accordion item's header.
Any thoughts?
You should assign the object to the Header property instead.
Try
item.Header = Case;
精彩评论