我在 ListBox ItemControl.ItemTemplate 中有一个 Expander。数据绑定到 ListBox 后,每个 ListItem 上的所有扩展器都具有 IsExpanded = False。当手动将新的 ListItem 添加到 ListBox 时,我需要将 IsExpanded 默认设置为 true。我的 XAML 如下:
<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 上设置绑定,因此在手动打开新项目时确实如此。
感谢您提供任何帮助!