WPF工具栏项Horizo​​ntalAligment ="右"

jsm*_*ith 13 wpf xaml toolbar alignment

是否可以使WPF工具栏中的元素具有RightAlignment为Right?

<ToolBar Height="38" VerticalAlignment="Top" Grid.Row="1">
    <Button HorizontalAlignment="Left" Width="50" VerticalAlignment="Stretch"/>
    <Button HorizontalAlignment="Left" Width="50" VerticalAlignment="Stretch"/>
    <ComboBox Width="120" HorizontalAlignment="Right"/>
</ToolBar>
Run Code Online (Sandbox Code Playgroud)

我已经尝试将元素添加到Grid中,并将ColumnDefinitions 分配给Left/Right.我也试过了StackPanel.无论我尝试什么,我似乎无法让ComboBox"锚定"在工具栏的右侧.

更新:

<DockPanel LastChildFill="True">
Run Code Online (Sandbox Code Playgroud)

不起作用,它不会像普通元素那样填充ToolBar元素.

jsm*_*ith 16

进一步的调查显示,为了做到这一点,我需要设置一个Grid内部的宽度ToolBar,或者像克里斯尼科尔所说的那样,DockPanelToolBar动态范围内Toolbar使用宽度RelativeSource.

但是,这不是一个干净的解决方案.Toolbar在调整大小时正确更新是非常复杂的.所以相反,我发现了一些看起来很糟糕的黑客行为.

<ToolBar Height="38" VerticalAlignment="Top" Grid.Row="1">
    <Button HorizontalAlignment="Left" Width="50" VerticalAlignment="Stretch"/>
    <Button HorizontalAlignment="Left" Width="50" VerticalAlignment="Stretch"/>
</ToolBar>

<ComboBox Margin="0,0,15,0" Width="120" HorizontalAlignment="Right" Grid.Row="1"/>
Run Code Online (Sandbox Code Playgroud)

因为我所有的元素都在一个网格,我可以把我ComboBox在顶部ToolBar通过分配它的Grid.Row同一行的工具栏.设置我稍微MarginsComboBox过来以免干扰外观,它根据需要操作没有错误.由于我发现这样做的唯一另一种方法是动态设置DockPanel/Grid的Width属性,我实际上觉得这是更清洁更有效的方法.


Leo*_*ast 5

我意识到这是一个古老的话题,但在提出问题时它仍然会弹出。我是这样处理这个问题的:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition x:Name="MenuRow"    Height="25"/>
        <RowDefinition x:Name="ToolbarRow" Height="25"/>
        <RowDefinition x:Name="CatalogRow" Height="*"/>
        <RowDefinition x:Name="RecipeRow"  Height="0.4*"/>
    </Grid.RowDefinitions>
    <ToolBar Grid.Row="1">
        <Button x:Name="tbFileOpen" Margin="0,0,0,0" Click="MenuItem_Click"><Image Source="Icons/Main/File/Load.png"/></Button>
        <Button x:Name="tbFileSave" Margin="0,0,0,0" Click="MenuItem_Click"><Image Source="Icons/Main/File/Save.png"/></Button>
        <Button x:Name="tbFileClear" Margin="0,0,0,0" Click="MenuItem_Click"><Image Source="Icons/Main/File/New document.png"/></Button>
    </ToolBar>
    <ToolBar Grid.Row="1" HorizontalAlignment="Right">
        <Button x:Name="tbFileExit" Margin="0,0,0,0" Click="MenuItem_Click"><Image Source="Icons/Main/File/Exit.png"/></Button>
    </ToolBar>
</Grid>
Run Code Online (Sandbox Code Playgroud)

有效:我创建了两个工具栏对象并将它们放在同一个 Grid.row 上。第一个默认(左)对齐,第二个是右对齐。它似乎对我有用。


dot*_*NET 5

对于寻找解决方案的其他人,以下对我有用:

<ToolBar HorizontalAlignment="Stretch" ToolBarTray.IsLocked="True">
  <ToolBar.Resources>
    <Style TargetType="{x:Type DockPanel}">
      <Setter Property="HorizontalAlignment" Value="Right" />
    </Style>
</ToolBar.Resources>
Run Code Online (Sandbox Code Playgroud)

我正在使用 .NET 4.6 和 VS2015,但我相信这也适用于以前的版本。