2013-10-01 98 views
0

我正在制作一种导航栏,使用水平方向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> 

这可能是一个非常愚蠢的问题,但它确实让我感到困扰,我有点卡住了。

回答

1

所以我能够通过简单地添加一个TextBlock到ListItem模板来解决这个问题。

我可以将现在空白的ToggleButton拉伸到ListItem的完整大小,然后用TextBlock居中/做任何事情。

这不是一个理想的实现,但它是我现在能想到的最好的一个。

<ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
      <TextBlock Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
      <ToggleButton VerticalAlignment="Stretch" 
         Content="" 
         IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" 
         Style="{StaticResource BlankButtonStyle}" 
         /> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
0

您可以使用VerticalContentAlignment属性。

<Style TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    ...