Silverlight 绑定 HeaderTemplate
我有一个手风琴控件,这是 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>
在我的用户控件资源中,我还有一个数据模板,我想将其用于每个手风琴项中的标题模板。
手风琴项目是从代码中填充的,我这样做是因为我动态接收它们。
代码如下:
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 的成员。 我将数据模板中的 xaml 中的这个成员绑定到文本块:
<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 仍然没有显示在 Accordion 项的标题中。
有什么想法吗?
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="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>
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将该对象分配给 Header 属性。
尝试
You should assign the object to the Header property instead.
Try