左右对齐在StackPanel内部留出空间

时间:2017-06-30 15:42:42

标签: wpf alignment

我有这个用户控件:

我想要达到这个结果:

底部的XAML是这样的:

<StackPanel HorizontalAlignment="left" Orientation="Horizontal">
     <TextBox materialDesign:HintAssist.Hint="Filtrar por patente" Width="auto" Margin="5" Text="{Binding Filtro, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left"></TextBox>
     <Button Width="120" Style="{DynamicResource MaterialDesignRaisedButton}" Margin="5" HorizontalAlignment="Right" Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}">Volver</Button>
</StackPanel>

我找不到一种方法将文本框对齐到左边,按钮向右对齐,在中间留下一个动态空间。按钮应具有固定宽度,文本框应为自动。

由于

1 个答案:

答案 0 :(得分:2)

使用DockPanel。您需要拉伸它而不是将其对齐到左侧。

<DockPanel 
    HorizontalAlignment="Stretch" 
    >
    <TextBox 
        DockPanel.Dock="Left"
        materialDesign:HintAssist.Hint="Filtrar por patente" 
        Width="auto" 
        Margin="5" 
        Text="{Binding Filtro, UpdateSourceTrigger=PropertyChanged}" 
        HorizontalAlignment="Left"
        />
    <Button 
        DockPanel.Dock="Right"
        Width="120" 
        Style="{DynamicResource MaterialDesignRaisedButton}" 
        Margin="5" 
        HorizontalAlignment="Right" 
        Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}"
        >Volver</Button>
</DockPanel>