WPF自定义ItemsControl - 换行问题

3
我想创建一个面包屑控件,在开头有一个按钮,用于清除面包屑项。我的问题在于如何让第一个按钮与其余项正确地自动换行。以下是我的样式:
<Style TargetType="{x:Type local:Breadcrumb}">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Button Content="{Binding}"
                        FontSize="{Binding RelativeSource={RelativeSource AncestorType=local:Breadcrumb}, Path=BreadcrumbFontSize}" 
                        FontFamily="{Binding RelativeSource={RelativeSource AncestorType=local:Breadcrumb}, Path=BreadcrumbFont}"
                        FontWeight="{Binding RelativeSource={RelativeSource AncestorType=local:Breadcrumb}, Path=BreadcrumbFontWeight}"
                        Width="{Binding RelativeSource={RelativeSource AncestorType=local:Breadcrumb}, Path=BreadcrumbItemWidth}"
                        Height="{Binding RelativeSource={RelativeSource AncestorType=local:Breadcrumb}, Path=BreadcrumbItemHeight}"
                        Margin="0,0,-22,10"
                        Style="{DynamicResource BreadcrumbButton}" />
            </DataTemplate>
        </Setter.Value>
    </Setter>

    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ItemsControl}">
                <WrapPanel>
                    <Button Content="Menu" Height="{Binding RelativeSource={RelativeSource AncestorType=local:Breadcrumb}, Path=BreadcrumbItemHeight}"
                            Width="{Binding RelativeSource={RelativeSource AncestorType=local:Breadcrumb}, Path=BreadcrumbItemWidth}"/>
                    <ItemsPresenter Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" />
                </WrapPanel> 
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

当所有项目都适合同一行时,它可以正确显示: enter image description here 当它开始换行时,它会将所有项目推到“菜单”按钮下方: enter image description here 有没有办法更新它,使其他元素不会在菜单按钮下面?

Chris,请问您能否发布一下您按钮的样式?我想创建类似的东西。 - Gail Foad
@GailBowen 抱歉,我不再拥有那段代码了。请在这里查看答案:https://dev59.com/_m025IYBdhLWcg3w6aQX - Chris Klepeis
1个回答

1
不,ItemsPresenter 将包含另一个 WrapPanel,我建议将该按钮作为项的一部分(例如使用 CompositeCollection),这样它就在同一个 WrapPanel 中。

有没有什么我可以在ItemsSource中覆盖的内容,以便将它添加到CollectionView中,这样它就不存在于绑定集合中,但是可以绑定到一个底层集合中,从而绘制控件? - Chris Klepeis
@ChrisKlepeis:我认为没有,我会使用CompositeCollection作为ItemsSource,将CollectionContainer绑定到您实际的项目上(这有点棘手)。 - H.B.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接