我在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有約束力的,所以當新的項目是手動開啓它是真實的頭部。
感謝您提供任何幫助!
您確定要爲此添加擴展器嗎?它將用完UI房地產。你有沒有用來考慮DataGrid – Paparazzi