我在Listbox中使用扩展器控件来显示项目的Hierarchical列表,我正确地获得了可见性。 这是我在xaml中用来绑定数据的代码
<Window.Resources>
<ControlTemplate x:Key="WithoutToggleButton" TargetType="{x:Type Expander}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="3" SnapsToDevicePixels="True">
<ToggleButton x:Name="HeaderSite" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" DockPanel.Dock="Top" Foreground="{TemplateBinding Foreground}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontStretch="{TemplateBinding FontStretch}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" MinWidth="0" MinHeight="0" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
<ToggleButton.Template>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Padding="{TemplateBinding Padding}">
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="Left" Margin="4,0,0,0" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
</Border>
</ControlTemplate>
<ControlTemplate x:Key="WithToggleButton" TargetType="{x:Type Expander}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="3" SnapsToDevicePixels="True">
<DockPanel>
<ToggleButton x:Name="HeaderSite" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" DockPanel.Dock="Top" Foreground="{TemplateBinding Foreground}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontStretch="{TemplateBinding FontStretch}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" MinWidth="0" MinHeight="0" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
<ToggleButton.FocusVisualStyle>
<Style>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border>
<Rectangle Margin="0" SnapsToDevicePixels="True" Stroke="Black" StrokeThickness="1" StrokeDashArray="1 2"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.FocusVisualStyle>
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Padding="{TemplateBinding Padding}">
<Grid Background="Transparent" SnapsToDevicePixels="False">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="19"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Ellipse x:Name="circle" Fill="White" HorizontalAlignment="Center" Height="19" Stroke="#FF333333" VerticalAlignment="Center" Width="19"/>
<Path x:Name="arrow" Data="M1,1.5L4.5,5 8,1.5" HorizontalAlignment="Center" SnapsToDevicePixels="False" Stroke="#FF333333" StrokeThickness="2" VerticalAlignment="Center"/>
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="Left" Margin="4,0,0,0" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Data" TargetName="arrow" Value="M1,4.5L4.5,1 8,4.5"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Stroke" TargetName="circle" Value="#FF5593FF"/>
<Setter Property="Fill" TargetName="circle" Value="#FFF3F9FF"/>
<Setter Property="Stroke" TargetName="arrow" Value="Black"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Stroke" TargetName="circle" Value="#FF3C77DD"/>
<Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
<Setter Property="Fill" TargetName="circle" Value="#FFD9ECFF"/>
<Setter Property="Stroke" TargetName="arrow" Value="Black"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Stroke" TargetName="circle" Value="#FFBCBCBC"/>
<Setter Property="Fill" TargetName="circle" Value="#FFE6E6E6"/>
<Setter Property="Stroke" TargetName="arrow" Value="#FF707070"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.Style>
</ToggleButton>
<ContentPresenter x:Name="ExpandSite" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" DockPanel.Dock="Bottom" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</DockPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
</Trigger>
<Trigger Property="ExpandDirection" Value="Right">
<Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Right"/>
<Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Left"/>
<Setter Property="Style" TargetName="HeaderSite">
<Setter.Value>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Padding="{TemplateBinding Padding}">
<Grid Background="Transparent" SnapsToDevicePixels="False">
<Grid.RowDefinitions>
<RowDefinition Height="19"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid>
<Grid.LayoutTransform>
<TransformGroup>
<RotateTransform Angle="-90"/>
</TransformGroup>
</Grid.LayoutTransform>
<Ellipse x:Name="circle" Fill="White" HorizontalAlignment="Center" Height="19" Stroke="#FF333333" VerticalAlignment="Center" Width="19"/>
<Path x:Name="arrow" Data="M1,1.5L4.5,5 8,1.5" HorizontalAlignment="Center" SnapsToDevicePixels="False" Stroke="#FF333333" StrokeThickness="2" VerticalAlignment="Center"/>
</Grid>
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="Center" Margin="0,4,0,0" Grid.Row="1" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Top"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Data" TargetName="arrow" Value="M1,4.5L4.5,1 8,4.5"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Stroke" TargetName="circle" Value="#FF5593FF"/>
<Setter Property="Fill" TargetName="circle" Value="#FFF3F9FF"/>
<Setter Property="Stroke" TargetName="arrow" Value="Black"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Stroke" TargetName="circle" Value="#FF3C77DD"/>
<Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
<Setter Property="Fill" TargetName="circle" Value="#FFD9ECFF"/>
<Setter Property="Stroke" TargetName="arrow" Value="Black"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Stroke" TargetName="circle" Value="#FFBCBCBC"/>
<Setter Property="Fill" TargetName="circle" Value="#FFE6E6E6"/>
<Setter Property="Stroke" TargetName="arrow" Value="#FF707070"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="ExpandDirection" Value="Up">
<Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Top"/>
<Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Bottom"/>
<Setter Property="Style" TargetName="HeaderSite">
<Setter.Value>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Padding="{TemplateBinding Padding}">
<Grid Background="Transparent" SnapsToDevicePixels="False">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="19"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid>
<Grid.LayoutTransform>
<TransformGroup>
<RotateTransform Angle="180"/>
</TransformGroup>
</Grid.LayoutTransform>
<Ellipse x:Name="circle" Fill="White" HorizontalAlignment="Center" Height="19" Stroke="#FF333333" VerticalAlignment="Center" Width="19"/>
<Path x:Name="arrow" Data="M1,1.5L4.5,5 8,1.5" HorizontalAlignment="Center" SnapsToDevicePixels="False" Stroke="#FF333333" StrokeThickness="2" VerticalAlignment="Center"/>
</Grid>
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="Left" Margin="4,0,0,0" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Data" TargetName="arrow" Value="M1,4.5L4.5,1 8,4.5"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Stroke" TargetName="circle" Value="#FF5593FF"/>
<Setter Property="Fill" TargetName="circle" Value="#FFF3F9FF"/>
<Setter Property="Stroke" TargetName="arrow" Value="Black"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Stroke" TargetName="circle" Value="#FF3C77DD"/>
<Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
<Setter Property="Fill" TargetName="circle" Value="#FFD9ECFF"/>
<Setter Property="Stroke" TargetName="arrow" Value="Black"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Stroke" TargetName="circle" Value="#FFBCBCBC"/>
<Setter Property="Fill" TargetName="circle" Value="#FFE6E6E6"/>
<Setter Property="Stroke" TargetName="arrow" Value="#FF707070"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="ExpandDirection" Value="Left">
<Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Left"/>
<Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Right"/>
<Setter Property="Style" TargetName="HeaderSite">
<Setter.Value>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Padding="{TemplateBinding Padding}">
<Grid Background="Transparent" SnapsToDevicePixels="False">
<Grid.RowDefinitions>
<RowDefinition Height="19"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid>
<Grid.LayoutTransform>
<TransformGroup>
<RotateTransform Angle="90"/>
</TransformGroup>
</Grid.LayoutTransform>
<Ellipse x:Name="circle" Fill="White" HorizontalAlignment="Center" Height="19" Stroke="#FF333333" VerticalAlignment="Center" Width="19"/>
<Path x:Name="arrow" Data="M1,1.5L4.5,5 8,1.5" HorizontalAlignment="Center" SnapsToDevicePixels="False" Stroke="#FF333333" StrokeThickness="2" VerticalAlignment="Center"/>
</Grid>
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="Center" Margin="0,4,0,0" Grid.Row="1" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Top"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Data" TargetName="arrow" Value="M1,4.5L4.5,1 8,4.5"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Stroke" TargetName="circle" Value="#FF5593FF"/>
<Setter Property="Fill" TargetName="circle" Value="#FFF3F9FF"/>
<Setter Property="Stroke" TargetName="arrow" Value="Black"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Stroke" TargetName="circle" Value="#FF3C77DD"/>
<Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
<Setter Property="Fill" TargetName="circle" Value="#FFD9ECFF"/>
<Setter Property="Stroke" TargetName="arrow" Value="Black"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Stroke" TargetName="circle" Value="#FFBCBCBC"/>
<Setter Property="Fill" TargetName="circle" Value="#FFE6E6E6"/>
<Setter Property="Stroke" TargetName="arrow" Value="#FF707070"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander ExpandDirection="Down"
IsExpanded="{Binding Mode=TwoWay, Path=IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}}" >
<Expander.Header>
<BulletDecorator>
<Button Tag="{Binding}" Style="{StaticResource ResourceKey=BookPageStyle}" Click="MainChapterButton_Click" Content="{Binding Path=name}" >
</Button>
</BulletDecorator>
</Expander.Header>
<StackPanel>
<ListBox x:Name="SubChapListBox" BorderThickness="0" Margin="20,0" ItemsSource="{Binding Path=chapters}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Expander ExpandDirection="Down" BorderThickness="0" >
<Expander.Header>
<BulletDecorator>
<Button Tag="{Binding}" Style="{StaticResource ResourceKey=BookPageStyle}" Click="SubchpaterButton_Click" Content="{Binding Path=name}" >
</Button>
</BulletDecorator>
</Expander.Header>
</Expander>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
问题是我得到Expander Bullet,即使扩展器中没有子项。我怎么能避免这种情况?
这是我想要做的。我可以通过XAML修改吗? 这是我试过的完整源代码的链接 https://onedrive.live.com/redir?resid=529BE5C1D41B4A28%21347
答案 0 :(得分:2)
编辑:如果要显示标题,可以定义两个不同的DataTemplates。在你的xaml中添加一个ContentControl:
<ContentControl Content="{Binding YourViewModel}"
ContentTemplateSelector="{StaticResource YourDataTemplateSelector }" />
此外,您还必须创建一个新的DataTemplateSelector
。如果您的集合不是空的,并且您的模板例如为空,则选择器应返回您的DataTemplate for Expander TextBlock如果你的收藏是空的。
public class YourDataTemplateSelector : DataTemplateSelector
{
#region Properties
public DataTemplate TextBoxTemplate
{
get;
set;
}
public DataTemplate ExpanderTemplate
{
get;
set;
}
public override Template SelectTemplate(object item, DependencyObject container)
{
// your logic do determine what template you need goes here
if (...) {
return TextBoxTemplate;
} else if (...){
return ExpanderTemplate;
}
}
}
以下是datatemplates的xaml主体:
<DataTemplateSelector x:Key="YourDataTemplateSelector"
TextBoxTemplate="{StaticResource ParameterTextBoxTemplate}"
ExpanderTemplate="{StaticResource ParameterExpanderTemplate}"/>
<DataTemplate x:Key="ParameterTextBoxTemplate">
...
</DataTemplate>
<DataTemplate x:Key="ParameterExpanderTemplate">
...
</DataTemplate>
答案 1 :(得分:2)
我为扩展器 WithToggleButton 和 WithToggleButton 设计了两个模板。
<Window.Resources>
<ControlTemplate x:Key="WithToggleButton" TargetType="{x:Type Expander}">
<!--Copy from msdn link as there is limitation to add code in stackoverflow-->
</ControlTemplate>
<ControlTemplate x:Key="WithoutToggleButton" TargetType="{x:Type Expander}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="3" SnapsToDevicePixels="True">
<DockPanel>
<ToggleButton x:Name="HeaderSite" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" DockPanel.Dock="Top" Foreground="{TemplateBinding Foreground}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontStretch="{TemplateBinding FontStretch}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" MinWidth="0" MinHeight="0" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Padding="{TemplateBinding Padding}">
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="Left" Margin="4,0,0,0" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.Style>
</ToggleButton>
<ContentPresenter x:Name="ExpandSite" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" DockPanel.Dock="Bottom" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</DockPanel>
</Border>
</ControlTemplate>
</Window.Resources>
<StackPanel>
<Expander Header="ExpanderWithoutContent" x:Name="Expander">
<Expander.Style>
<Style TargetType="Expander">
<Setter Property="Template" Value="{StaticResource WithToggleButton}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Content,ElementName=Expander}" Value="{x:Null}">
<Setter Property="Template" Value="{StaticResource WithoutToggleButton}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
</Expander>
<Expander x:Name="Expander1" Header="ExpanderWithContent" >
<Expander.Style>
<Style TargetType="Expander">
<Setter Property="Template" Value="{StaticResource WithToggleButton}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Content,ElementName=Expander1}" Value="{x:Null}">
<Setter Property="Template" Value="{StaticResource WithoutToggleButton}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
<Expander.Content>
<Grid Height="50" Background="Green">
<TextBlock Text="expander content" Foreground="White" FontSize="20" VerticalAlignment="Center"></TextBlock>
</Grid>
</Expander.Content>
</Expander>
</StackPanel>
<强>结果强>
<强> UPADTE 强>
Plesae在现有窗口资源中添加以上扩展程序模板 WithToggleButton 和 WithoutToggleButton ,并使用以下新的xaml代码替换您的网格。
<Grid >
<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="10" Background="White">
<Grid>
<Label Name="lblstatus" HorizontalAlignment="center" VerticalAlignment="top" Margin="10" FontSize="14" FontWeight="bold" Foreground="Black" >Please wait...</Label>
<ListBox ItemsSource="{Binding}" Name="TOCView" ItemContainerStyle="{StaticResource ListBoxItemStyle}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander x:Name="Expander" ExpandDirection="Down" IsExpanded="{Binding Mode=TwoWay, Path=IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}}" >
<Expander.Style>
<Style TargetType="Expander" BasedOn="{StaticResource ExpanderItemStyle}">
<Setter Property="Template" Value="{StaticResource WithToggleButton}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=chapters}" Value="{x:Null}">
<Setter Property="Template" Value="{StaticResource WithoutToggleButton}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
<Expander.Header>
<BulletDecorator>
<Button Tag="{Binding}" Style="{StaticResource ResourceKey=BookPageStyle}" Click="MainChapterButton_Click" Content="{Binding Path=name}"/>
</BulletDecorator>
</Expander.Header>
<Expander.Content>
<StackPanel>
<ListBox x:Name="SubChapListBox" BorderThickness="0" Margin="20,0" ItemsSource="{Binding Path=chapters}" ItemContainerStyle="{StaticResource ListBoxItemStyle}">
<ListBox.ItemTemplate>
<DataTemplate>
<Expander Name="Expander1" ExpandDirection="Down" BorderThickness="0">
<Expander.Style>
<Style TargetType="Expander" BasedOn="{StaticResource ExpanderItemStyle}">
<Setter Property="Template" Value="{StaticResource WithToggleButton}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=chapters}" Value="{x:Null}">
<Setter Property="Template" Value="{StaticResource WithoutToggleButton}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
<Expander.Header>
<BulletDecorator>
<Button Tag="{Binding}" Style="{StaticResource ResourceKey=BookPageStyle}" Click="SubchpaterButton_Click" Content="{Binding Path=name}"/>
</BulletDecorator>
</Expander.Header>
<StackPanel>
<ListBox x:Name="SubChapListBox" BorderThickness="0" Margin="20,0" ItemsSource="{Binding Path=chapters}" ItemContainerStyle="{StaticResource ListBoxItemStyle}">
<ListBox.ItemTemplate>
<DataTemplate>
<Expander Name="expander2" ExpandDirection="Down" BorderThickness="0" >
<Expander.Style>
<Style TargetType="Expander">
<Setter Property="Template" Value="{StaticResource WithToggleButton}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=chapters}" Value="{x:Null}">
<Setter Property="Template" Value="{StaticResource WithoutToggleButton}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
<Expander.Header>
<BulletDecorator>
<Button Tag="{Binding}" Style="{StaticResource ResourceKey=BookPageStyle}" Click="SubchpaterButton_Click" Content="{Binding Path=name}"/>
</BulletDecorator>
</Expander.Header>
</Expander>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Expander>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Expander.Content>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
</Grid>
</ScrollViewer>
</Grid>
<强>结果强>
答案 2 :(得分:0)
您希望使用默认BooleanToVisibilityConverter
绑定到ListBox
。首先,您需要添加资源:
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
然后您可以绑定到HasItems
ListBox
属性
<Expander ExpandDirection="Down"
IsExpanded="{Binding Mode=TwoWay, Path=IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}}"
Visibility="{Binding Path=HasItems, ElementName=SubChapListBox, Converter={StaticResource BooleanToVisibilityConverter}}">
修改强>
如上所述,这将隐藏包括标题的完整扩展器。但您可以在BulletDecorator
(或其中的Button
内容)中使用相同的绑定代替:
<Expander.Header>
<BulletDecorator Visibility="{Binding Path=HasItems, ElementName=SubChapListBox, Converter={StaticResource BooleanToVisibilityConverter}}">
<Button Tag="{Binding}" Style="{StaticResource ResourceKey=BookPageStyle}" Click="MainChapterButton_Click" Content="{Binding Path=name}" >
</Button>
</BulletDecorator>
</Expander.Header>
答案 3 :(得分:0)
这是用于DataGrid分组的解决方案,但相同的想法也适用于ListView。创建两个ControlTemplate作为资源。
<Window.Resources>
<ControlTemplate x:Key="DataGridGroupExpender" TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Background="LightGray" BorderThickness="1,1,1,1">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0"/>
<TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount, StringFormat=: {0}}"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
<ControlTemplate x:Key="DataGridGroupNoExpender" TargetType="{x:Type GroupItem}">
<ItemsPresenter />
</ControlTemplate>
</Window.Resources>
使用DataTrigger基于组中的项目数切换ContainerStyle:1表示没有子项,因此没有Expander。
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,5"/>
<Setter Property="Template" Value="{StaticResource DataGridGroupExpender}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ItemCount}" Value="1">
<Setter Property="Template" Value="{StaticResource DataGridGroupNoExpender}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>