这是我第一次使用WPF,我有一个带有ToolBar的usercontrol,以及ToolBar上的ToolBarPanels。我的问题是尝试右对齐包含一些按钮的最后一个ToolBarPanel。
我已经搜索了互联网并尝试了解决方案(将其包裹在网格,dockpanel,stackpanel中),但没有一个有效。
我的XAML是:
<ToolBar
HorizontalAlignment="Left" Width="{Binding ActualWidth, ElementName=userControl}"
VerticalAlignment="Top"
Name="ToolBarPanel1"
UseLayoutRounding="False"
ToolBarTray.IsLocked="True">
<ToolBar.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF475B7E" Offset="1"/>
<GradientStop Color="#FFB9C3D5" Offset="0.427"/>
</LinearGradientBrush>
</ToolBar.Background>
.........
<ToolBarPanel ToolBar.OverflowMode="AsNeeded" Orientation="Horizontal" HorizontalAlignment="Right" >
<Button
Name="btnPrint"
Margin="5"
HorizontalAlignment="Right"
VerticalAlignment="Center"
BorderThickness="1"
IsEnabled="{Binding Path=AllowEdit}"
BorderBrush="#FF586A8B"
DockPanel.Dock="Right"
Visibility="{Binding Path=ControlVisibility.Print,
Converter={StaticResource BooleanToVisibilityConverter}}">
<Image Source="/component/Images/print-image.png"></Image>
<Button.ToolTip>
<ToolTip>
<TextBlock>Print Chart</TextBlock>
</ToolTip>
</Button.ToolTip>
</Button>
<Button
Name="btnCopy"
Margin="5"
HorizontalAlignment="Right"
VerticalAlignment="Center"
BorderThickness="1"
IsEnabled="{Binding Path=AllowEdit}"
BorderBrush="#FF586A8B"
Visibility="{Binding Path=ControlVisibility.Copy,
Converter={StaticResource BooleanToVisibilityConverter}}">
<Image Source="/component/Images/copy-image.png"></Image>
<Button.ToolTip>
<ToolTip>
<TextBlock>Copy Chart to Clipboard</TextBlock>
</ToolTip>
</Button.ToolTip>
</Button>
<Button
Name="btnInfo"
Margin="5"
HorizontalAlignment="Right"
VerticalAlignment="Center"
BorderThickness="1"
IsEnabled="{Binding Path=AllowEdit}"
BorderBrush="#FF586A8B"
Visibility="{Binding Path=ControlVisibility.Info,
Converter={StaticResource BooleanToVisibilityConverter}}">
<Image Source="/component/Images/help-image.png"></Image>
<Button.ToolTip>
<ToolTip>
<TextBlock>Chart Help</TextBlock>
</ToolTip>
</Button.ToolTip>
</Button>
</ToolBarPanel>
</ToolBar>
答案 0 :(得分:1)
将最后一列定义设置为“*”的拉伸网格应该可以实现您的目标。您也可以尝试使用DockPanel并像这样对齐:
<DockPanel>
<ToolBarPanel DockPanel.Dock="Left"/>
<ToolBarPanel DockPanel.Dock="Left"/>
<ToolBarPanel DockPanel.Dock="Left"/>
<!-- Last One -->
<ToolBarPanel DockPanel.Dock="Right"/>
</DockPanel>