2017-10-19 23 views
0

J'ai un ItemsControl, le ItemsPanel est faite par un DockPanel.Remplissez tout l'espace disponible dans ItemsComtrol avec la même largeur largeur des éléments

À l'intérieur du DockPanel, je peux avoir un, deux ou trois boutons. Le problème provient de la largeur des boutons: je veux les trois éléments de la même taille, mais les éléments prennent la taille dont ils ont besoin (le dernier élément prend l'excès de largeur car LastChildFill est vrai). Puis-je donner aux boutons la même largeur sans fournir leur taille manuellement?

<ItemsControl ItemTemplate="{StaticResource Template1}" ItemsSource="{Binding Path=options, Mode=OneWay}" ItemsPanel="{StaticResource Panel1}" HorizontalContentAlignment="Stretch"/> 

    <ItemsPanelTemplate x:Key="Panel1"> 
     <DockPanel Height="Auto" Width="Auto" LastChildFill="True"/> 
    </ItemsPanelTemplate> 

    <DataTemplate x:Key="BasicasTemplateOpciones" DataType="{x:Type local:MyOption}"> 
     <Grid HorizontalAlignment="Stretch"> 
      <Button DataContext="{Binding}" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > 
       <Button.Template> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <Grid HorizontalAlignment="Stretch" VerticalAlignment="Center"> 
           <StackPanel Orientation="Horizontal"> 
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Stretch"/> 
            <TextBlock HorizontalAlignment="Right" VerticalAlignment="Stretch"/> 
           </StackPanel> 
          </Grid> 
        </ControlTemplate> 
       </Button.Template> 
      </Button> 
     </Grid> 
    </DataTemplate> 

Répondre

2

A UniformGrid avec une seule ligne va faire ce que vous voulez:

<ItemsPanelTemplate x:Key="Panel1"> 
    <UniformGrid Rows="1" /> 
</ItemsPanelTemplate> 

Exemple:

enter image description here

<StackPanel Orientation="Vertical"> 
    <StackPanel.Resources> 
     <ItemsPanelTemplate x:Key="Panel1"> 
      <UniformGrid Rows="1" /> 
     </ItemsPanelTemplate> 
     <Style TargetType="ItemsControl" x:Key="ICStyle"> 
      <Style.Resources> 
       <Style TargetType="Button"> 
        <Setter Property="Margin" Value="2" /> 
       </Style> 
      </Style.Resources> 
      <Setter Property="ItemsPanel" Value="{StaticResource Panel1}" /> 
     </Style> 
    </StackPanel.Resources> 

    <ItemsControl Style="{StaticResource ICStyle}"> 
     <Button>Foo</Button> 
    </ItemsControl> 

    <ItemsControl Style="{StaticResource ICStyle}"> 
     <Button>Foo</Button> 
     <Button>Bar</Button> 
    </ItemsControl> 

    <ItemsControl Style="{StaticResource ICStyle}"> 
     <Button>Foo</Button> 
     <Button>Bar</Button> 
     <Button>Baz</Button> 
    </ItemsControl> 
</StackPanel>