【问题标题】:WPF Custom ItemsControl - Wrapping issueWPF 自定义 ItemsControl - 包装问题
【发布时间】:2012-07-17 14:01:58
【问题描述】:

我想创建一个面包屑控件,它的开头有一个按钮,用于清除面包屑项目。我的问题是让第一个按钮与其余项目正确包装。这是我的风格:

<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>

当所有项目都放在同一行时,它会正确显示:

当它开始换行时,它会推动“菜单”按钮下方的所有项目:

有没有办法可以更新它,使其他元素不会出现在菜单按钮下方?

【问题讨论】:

标签: wpf controltemplate itemscontrol wrappanel


【解决方案1】:

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

【讨论】:

  • 有什么我可以在 ItemsSource 中重写以将其添加到 CollectionView 中,以便它不存在于绑定集合中,但绑定在绘制控件的基础集合中?
  • @ChrisKlepeis:不认为有,我会使用CompositeCollection 作为ItemsSource,将CollectionContainer (it's a bit tricky) 绑定到您的实际项目。
猜你喜欢
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多