我正在制作一种导航栏,使用水平方向ListBox
(可能有更好的方法),因为我需要一次只能选择一件东西,并保持选中状态,然后才能取消选择它。所以为了做到这一点,我使用了ToggleButton
。拉伸和中心对齐?
问题是,我'拉伸'的内容,以适应框中,使我可以点击选择区域的任何地方取消选择使用togglebutton。
我的问题是,我的按钮现在顶部对齐,因为拉伸,是否有可能垂直居中,而使用拉伸?每当我尝试将它们置于同一中心时,它会让我回到原来的位置,在那里按钮本身再次收缩到中心位置。
<ListBox Grid.Row="0" Grid.Column="0" ItemsSource="{Binding PageViewModels}" SelectedItem="{Binding CurrentPageViewModel}" Style="{StaticResource MenuStyle}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Padding" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FCCC"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#FAAA"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<ToggleButton VerticalAlignment="Stretch"
Content="{Binding Name}"
IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
Style="{StaticResource BlankButtonStyle}"
/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这可能是一个非常愚蠢的问题,但它确实让我感到困扰,我有点卡住了。