2012-07-17 62 views
3

我想创建一个面包屑控件,该控件在开始时会有一个按钮来清除面包屑项目。我的问题是让第一个按钮正确包装其余的项目。这是我的风格:WPF自定义ItemsControl - 包装问题

<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

有没有办法,我可以更新这个让其他元素不走下面的一种方式菜单按钮?

+0

[Chris](https://stackoverflow.com/users/71904/chris-klepeis),请你可以发布你的按钮样式?我想创建类似的东西。 – 2017-08-05 07:32:19

+0

@GailBowen对不起,我没有这个代码了。看看这里的答案https://stackoverflow.com/questions/5727382/breadcrumb-style-with-wpf-listview – 2017-08-07 13:39:52

回答

1

不,ItemsPresenter将包含另一个WrapPanel,我会建议使项目的按钮部分(例如使用CompositeCollection),那么它是在同一WrapPanel

+0

有什么我可以重写在ItemsSource添加到可能是一个CollectionView,使它不存在绑定的集合,但绑定在绘制控件的底层集合中? – 2012-07-17 14:08:17

+0

@ChrisKlepeis:不要以为有,我会使用一个'CompositeCollection'作为'ItemsSource',绑定一个'CollectionContainer'([有点棘手](http://stackoverflow.com/questions/6446699/how- do-you-bind-a-collectioncontainer-to-a-collection-in-a-view-model/6446923#6446923))到你的实际项目。 – 2012-07-17 14:09:44