2012-07-17 82 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