2010-01-03 115 views
1

我有一个使用数据模板的列表框。我需要的是在选择项目后的一种方式我想缩小列表框本身,而不是内部的列表项。我已经在selector.selected和unselected上尝试了eventtrigger,但它不会触发。我也在datatemplate上放了一个datatrigger,但是我无法从这里访问列表框。有任何想法吗?项目选择收缩列表框

回答

1

这是一个稍微间接的解决方案,但是...您可以通过将DataTrigger放置在ListBox本身上并绑定到SelectedItems.Count来处理此问题。您需要将ListBox默认设置为其“较小”外观。然后,触发器将检查SelectedItems.Count是否为0,如果是,则必须使列表框大于。在下面的例子中,为了简单起见,我设置了ListBox.Background,但是你应该能够使它适应LayoutTransform,RenderTransform或者Width/Height,或者你用来“收缩”ListBox的任何东西:

<ListBox.Style> 
    <Style TargetType="ListBox"> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding SelectedItems.Count, RelativeSource={RelativeSource Self}}" Value="0"> 
     <Setter Property="Background" Value="Orange" /> 
     </DataTrigger> 
    </Style.Triggers> 
    </Style> 
</ListBox.Style> 

显然这将缩小(或者,在我简化的例子中,变白)整个列表框选择任何东西时。要使选定的ListBoxItem保持完整大小,请使用ListBox.ItemContainerStyle。在这种情况下,你可以在IsSelected上触发,并应用合适的setter来反转“收缩”转换 - 例如应用负值保证金或反向ScaleTransform。 (普通触发器将为此做。)

0

首先,正确的事件挂钩的就是SelectionChangedSelected,其次,你可以在窗口级别使用Storyboard

Storyboard

<Storyboard x:Key="Storyboard1"> 
    <DoubleAnimationUsingKeyFrames 
     BeginTime="00:00:00" 
     Storyboard.TargetName="grid" 
     Storyboard.TargetProperty="(FrameworkElement.Height)"> 
     <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="0"/> 
    </DoubleAnimationUsingKeyFrames> 
</Storyboard> 

窗口触发:

<Window.Triggers> 
    <EventTrigger RoutedEvent="Selector.SelectionChanged" SourceName="listBox"> 
     <BeginStoryboard Storyboard="{StaticResource Storyboard1}"/> 
    </EventTrigger> 
</Window.Triggers> 

而且ListBox(有一些装饰贴效果):

<Border 
    BorderThickness="2" 
    CornerRadius="3" 
    BorderBrush="#FF000000" 
    Padding="3" 
    VerticalAlignment="Top"> 
    <Grid Height="200" x:Name="grid"> 
     <ListBox x:Name="listBox" Height="200"> 
      <ListBoxItem Content="ListBoxItem"/> 
      <ListBoxItem Content="ListBoxItem"/> 
      <ListBoxItem Content="ListBoxItem"/> 
      <ListBoxItem Content="ListBoxItem"/> 
     </ListBox> 
    </Grid> 
</Border> 
相关问题