如何在 WPF 中的 ListBox 上添加标签?
Posted
技术标签:
【中文标题】如何在 WPF 中的 ListBox 上添加标签?【英文标题】:How do I add a Label over a ListBox in WPF? 【发布时间】:2018-06-06 08:05:01 【问题描述】:我目前在DockPanel
中有一个ListBox
(代表精彩的 Diaballik 游戏),最后我希望能够在由图片中的盒子:
<DockPanel>
<ListBox DockPanel.Dock="Top" ItemsSource="Binding Cases" SelectedItem="Binding SelectedCase, Mode=TwoWay" HorizontalAlignment="Center" VerticalAlignment="Top" SnapsToDevicePixels="True">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="Binding Size"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid >
<Ellipse Fill="Binding Color" Width="50" Height="50" Visibility="Binding HasPawn, Converter=StaticResource bool2visibility"/>
...
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Label Content="VICTORY" /> <!-- How can this go over the list box ? -->
</DockPanel>
【问题讨论】:
【参考方案1】:你可以做这样的事情(Grid
的行为是它只会将项目“堆叠”在彼此的“顶部”):
<DockPanel>
<Grid DockPanel.Dock="Top">
<ListBox ItemsSource="Binding Cases" SelectedItem="Binding SelectedCase, Mode=TwoWay" HorizontalAlignment="Center" VerticalAlignment="Top" SnapsToDevicePixels="True">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="Binding Size"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid >
<Ellipse Fill="Binding Color" Width="50" Height="50" Visibility="Binding HasPawn, Converter=StaticResource bool2visibility"/>
...
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Label Content="VICTORY" HorizontalAlignment="Center" VerticalAlignment="Center" /> <!-- How can this go over the list box ? -->
</Grid>
</DockPanel>
【讨论】:
以上是关于如何在 WPF 中的 ListBox 上添加标签?的主要内容,如果未能解决你的问题,请参考以下文章