重写expander模板
功能:带checkbox的expander,如图所示,遗憾的是check的功能还未实现,要去忙其他事了,记录一下,还没想明白,后面补
<ListView ItemsSource="{Binding ExportReportInfos, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}">
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel>
<DockPanel.Resources>
<PathGeometry x:Key="upPath" Figures="M768 512 256 0 256 1024Z" />
<PathGeometry x:Key="downPath" Figures="M151.477 199.554h718.53c39.764 0 71.923 31.91 71.923 71.675 0 18.485-7.096 35.206-18.486 47.873l-355.33 474.125c-23.81 31.4-68.641 37.993-100.297 14.183-5.571-4.052-10.385-8.873-14.183-14.19L94.235 314.04C70.688 282.634 77.018 237.561 108.672 214c12.922-9.881 27.865-14.439 42.805-14.439v-0.007z m0 0" />
<Style x:Key="expanderStyle" TargetType="{x:Type Expander}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Expander">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<ToggleButton IsChecked="{Binding IsChecked}">
<ToggleButton.Template>
<ControlTemplate>
<Border Background="Transparent">
<Path
x:Name="path"
Width="10"
Height="10"
Margin="10,4"
Data="{StaticResource upPath}"
Fill="Black"
Stretch="Fill" />
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsChecked}" Value="True">
<Setter TargetName="path" Property="Data" Value="{StaticResource downPath}" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
<CheckBox Content="{Binding ReportName}" />
</StackPanel>
<ContentPresenter
x:Name="ExpandSite"
Grid.Row="1"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Focusable="False"
Visibility="Collapsed" />
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsChecked}" Value="True">
<Setter TargetName="ExpandSite" Property="Visibility" Value="Visible" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DockPanel.Resources>
<Expander
Padding="24,0"
DockPanel.Dock="Top"
Header="{Binding ReportName}"
IsExpanded="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource expanderStyle}">
<ItemsControl
x:Name="reportList"
VerticalAlignment="Stretch"
BorderThickness="1"
ItemsSource="{Binding SheetsInfo}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel
MinHeight="30"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Orientation="Horizontal">
<CheckBox Margin="5,5" IsChecked="{Binding IsChecked}" />
<TextBlock VerticalAlignment="Center" Text="{Binding SheetName}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Expander>
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
数据结构
public class ExportReportModel : ViewModelBase
{
private bool _isChecked;
public bool IsChecked {
get { return _isChecked; }
set {
_isChecked = value;
this.RaisePropertyChanged(nameof(IsChecked));
}
}
public string ReportName { get; set; }
public List<SheetModel> SheetsInfo { get; set; }
}
public class SheetModel : ViewModelBase
{
private bool _isChecked;
public bool IsChecked {
get { return _isChecked; }
set {
_isChecked = value;
this.RaisePropertyChanged(nameof(IsChecked));
}
}
public string SheetName { get; set; }
}