2010-01-03 103 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>