手风琴控件仅在第二次选择后显示项目内容

发布于 2024-12-13 12:20:57 字数 3157 浏览 0 评论 0原文

我有一个 AccordionControl (WPFToolkit),可以动态添加项目:

        <my:Accordion Grid.Column="1" 
                      ItemsSource="{Binding Path=Tests}" 
                      SelectionMode="ZeroOrOne" 
                      HorizontalAlignment="Stretch" 
                      VerticalAlignment="Stretch">
            <my:Accordion.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Header}" />
                    </StackPanel>
                </DataTemplate>
            </my:Accordion.ItemTemplate>
            <my:Accordion.ContentTemplate>
                <DataTemplate>
                    <ContentPresenter Content="{Binding Content}"/>
                </DataTemplate>
            </my:Accordion.ContentTemplate>
        </my:Accordion>

要实际查看内容,我必须选择(打开)一个 AccordionItem,关闭它并再次打开它。这种行为的原因可能是什么?

编辑 我已经找到了一种使用样式来解决这个问题的方法,但我仍然感兴趣为什么上面的方法不起作用。这里是样式解决方案:

        <Style x:Key="itemStyle" TargetType="my:AccordionItem">
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding Header}"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="accordionStyle" TargetType="my:Accordion">
        <Setter Property="ItemContainerStyle" Value="{StaticResource itemStyle}" />
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <ItemsControl ItemsSource="{Binding MenuItems}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=Text, Mode=OneWay}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

<my:Accordion Grid.Column="1" Height="Auto"
                         Name="accordion1" 
                         ExpandDirection="Left" 
                         SelectionMode="One"
                         ItemsSource="{Binding Tests}"
                         Style="{StaticResource accordionStyle}">
        </my:Accordion>

编辑 我现在发现了问题:我无法“拉伸 AccordionControl”。

<my:Accordion Grid.Column="1" Height="Auto"
                      VerticalAlignment="Stretch"
                      HorizontalAlignment="Stretch"
                         Name="accordion1" 
                         ExpandDirection="Left" 
                         SelectionMode="One"
                         ItemsSource="{Binding Tests}"
                         Style="{StaticResource accordionStyle}">
        </my:Accordion>

一旦我这样做,它就不再工作了。有人知道解决这个问题的方法吗?

I have an AccordionControl (WPFToolkit) to which I add items dynamically:

        <my:Accordion Grid.Column="1" 
                      ItemsSource="{Binding Path=Tests}" 
                      SelectionMode="ZeroOrOne" 
                      HorizontalAlignment="Stretch" 
                      VerticalAlignment="Stretch">
            <my:Accordion.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Header}" />
                    </StackPanel>
                </DataTemplate>
            </my:Accordion.ItemTemplate>
            <my:Accordion.ContentTemplate>
                <DataTemplate>
                    <ContentPresenter Content="{Binding Content}"/>
                </DataTemplate>
            </my:Accordion.ContentTemplate>
        </my:Accordion>

To actually see the Content, I have to select (open) an AccordionItem, close it and open it again. What could be the reason for this behaviour?

EDIT
I have found a way around this using styles instead, still I would be interested why the above does not work. Here the style solution:

        <Style x:Key="itemStyle" TargetType="my:AccordionItem">
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding Header}"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="accordionStyle" TargetType="my:Accordion">
        <Setter Property="ItemContainerStyle" Value="{StaticResource itemStyle}" />
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <ItemsControl ItemsSource="{Binding MenuItems}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=Text, Mode=OneWay}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

<my:Accordion Grid.Column="1" Height="Auto"
                         Name="accordion1" 
                         ExpandDirection="Left" 
                         SelectionMode="One"
                         ItemsSource="{Binding Tests}"
                         Style="{StaticResource accordionStyle}">
        </my:Accordion>

EDIT
I have now found the problem: I cannot "Stretch the AccordionControl.

<my:Accordion Grid.Column="1" Height="Auto"
                      VerticalAlignment="Stretch"
                      HorizontalAlignment="Stretch"
                         Name="accordion1" 
                         ExpandDirection="Left" 
                         SelectionMode="One"
                         ItemsSource="{Binding Tests}"
                         Style="{StaticResource accordionStyle}">
        </my:Accordion>

As soon as I do this, it is not working anymore. Does someone know a way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文