Silverlight 绑定 HeaderTemplate

发布于 2024-12-05 02:07:32 字数 2907 浏览 0 评论 0原文

我有一个手风琴控件,这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆梦 2024-12-12 02:07:32

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

尝试

item.Header = Case;

You should assign the object to the Header property instead.

Try

item.Header = Case;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文