Silverlight绑定HeaderTemplate

时间:2011-09-19 09:47:39

标签: silverlight binding

我有一个手风琴控制,这是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="http://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>

在我的用户控件资源中,我还有一个数据模板,我想在每个手风琴项目中用​​于headertemplate。

手风琴项目是从代码填充的,我这样做是因为我动态地接收它们。

以下是代码:

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"]; 



        }

类ECase有一个名为CaseName的成员。 我将datatemplate中的xaml中的此成员绑定到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>

我还将accordionItem的DataContext分配给ECase:

item.DataContext = Case;

但仍然没有在手风琴项目的标题中显示CaseName。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您应该将对象分配给Header属性。

尝试

item.Header = Case;