[英]How do I attach a label to the menu in WPF?
I want to attach Label
to the Menu
, where I have components of MenuItem
.我想将
Label
附加到Menu
,其中我有MenuItem
的组件。 I need to hide some MenuItem
s (I use Visibility.Collapsed
).我需要隐藏一些
MenuItem
(我使用Visibility.Collapsed
)。 When I do it, labels move.当我这样做时,标签会移动。 How to fit it?
怎么装?
<Menu x:Name="Menu" DockPanel.Dock="Top">
<MenuItem Name="MenuStartGame" Header="Start new game" FontSize="16" Click="MenuStartGame_Click">
</MenuItem>
<MenuItem Name="MenuCancelMyTurn" Header="Cancel my turn" FontSize="16" Visibility="Collapsed" Click="CancelMyTurn_Click">
</MenuItem>
<MenuItem Name="MenuAddButtons" Header="Add buttons" FontSize="16" Click="AddButtons_Click">
</MenuItem>
<MenuItem Name="MenuRemoveButtons" Header="Remove buttons" FontSize="16" Click="RemoveButtons_Click">
</MenuItem>
<Label x:Name="labelTime" HorizontalContentAlignment="Center" Content="" Width="100">
</Label>
<Label x:Name="labelScore" HorizontalContentAlignment="Center" Content="" Width="100">
</Label>
</Menu>
If you do not want labels to move when you hide or show the others, you should not use Visibility.Collapsed, use Visibility.Hidden.如果您不想在隐藏或显示其他标签时移动标签,则不应使用 Visibility.Collapsed,而应使用 Visibility.Hidden。
Maybe, if I understand correctly what you want to do, a Menu is not what you should use in your case.也许,如果我正确理解你想做什么,菜单不是你应该在你的情况下使用的。 You can use a Grid and have a greater control on what the dimensions of your columns will be.
您可以使用 Grid 并更好地控制列的尺寸。 Something like this:
是这样的:
<Grid x:Name="Menu" DockPanel.Dock="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Name="MenuStartGame" Header="Start new game" FontSize="16" Click="MenuStartGame_Click">
</Button>
<ButtonName="MenuCancelMyTurn" Grid.Column="1" Header="Cancel my turn" FontSize="16" Visibility="Collapsed" Click="CancelMyTurn_Click">
</Button>
<DockPanel Grid.Column="1">
<ButtonName="MenuAddButtons" Header="Add buttons" FontSize="16" Click="AddButtons_Click">
</Button>
<ButtonName="MenuRemoveButtons" Header="Remove buttons" FontSize="16" Click="RemoveButtons_Click">
</Button>
</DockPanel>
<Label Grid.Column="2" x:Name="labelTime" HorizontalContentAlignment="Center" Content="" Width="100">
</Label>
<Label Grid.Column="3" x:Name="labelScore" HorizontalContentAlignment="Center" Content="" Width="100">
</Label>
</Grid>
This is not tested, but should be what you need.这没有经过测试,但应该是您所需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.