2012-06-05 42 views
0

我在ListBox ItemControl.ItemTemplate中有一個Expander。數據綁定到ListBox後,每個ListItem上的所有展開器都有IsExpanded = False。當需要手動將新ListItem添加到ListBox時,我需要將IsExpanded默認值設置爲true。我的XAML如下:WPF Expander裏面ListBox ItemTemplate IsExpanded默認值

<ListBox 
    ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
    ScrollViewer.CanContentScroll="False" 
    VirtualizingStackPanel.IsVirtualizing="False" 
    Grid.ColumnSpan="2" 
    HorizontalAlignment="Stretch" 
    Grid.Row="2" 
    Name="ArbitraryDataListbox" 
    ItemsSource="{Binding ElementName=CurrentArbitraryDataListControl, Path=CurrentJob.AdditionalData}"> 
    <ListBox.Resources> 
     <Style TargetType="{x:Type Expander}"> 
      <Setter Property="IsExpanded" Value="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"/> 
      <Setter Property="BorderThickness" Value="1"/> 
      <Setter Property="BorderBrush" Value="Beige"/> 
      <Setter Property="Foreground" Value="#202020"/> 
      <Setter Property="Background" Value="Beige"/> 
     </Style> 
    </ListBox.Resources> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Expander Header="{Binding Path=Name}" Margin="0,8,0,0" IsExpanded="{Binding RelativeSource={RelativeSource self}, ElementName=ArbitraryDataListbox, Path=}"> 
       <Controls:ArbitraryDataControl 
        Width="{Binding ElementName=ArbitraryDataListbox, Path=ActualWidth, Converter={StaticResource SubtractConverter}, ConverterParameter=10}" 
        CurrentArbitraryData="{Binding}" 
        CurrentJob="{Binding ElementName=CurrentArbitraryDataListControl, Path=CurrentJob}"/> 
      </Expander> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ListBox> 

我很新的WPF和不能讓我圍繞着如何設置IsExpanded有約束力的,所以當新的項目是手動開啓它是真實的頭部。

感謝您提供任何幫助!

+0

您確定要爲此添加擴展器嗎?它將用完UI房地產。你有沒有用來考慮DataGrid – Paparazzi

回答

1

如果我理解你的代碼,現在你已經擴展了ListBox的SelectedItem,並且其他項目摺疊了。如果所選項目更改,則舊選定項目將摺疊,新選定項目將展開。

如果你想能夠添加一個項目到集合,然後選擇它,你應該考慮使用ListCollectionView。

ListCollectionView包裝你的內部集合並公開一個「CurrentItem」。您可以輕鬆地將此類綁定到您的ListBox,這將允許您在添加對象後調用ListCollectionView.MoveCurrentTo(object)來選擇對象。