忘記出處了........
自定義控件
public class TreeListView : TreeView
{
static TreeListView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TreeListView), new FrameworkPropertyMetadata(typeof(TreeListView)));
}
public TreeListView()
{
Columns = new GridViewColumnCollection();
}
public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns", typeof(GridViewColumnCollection), typeof(TreeListView), new UIPropertyMetadata(null));
public GridViewColumnCollection Columns
{
get { return (GridViewColumnCollection)GetValue(ColumnsProperty); }
set { SetValue(ColumnsProperty, value); }
}
}
<ControlTemplate TargetType="TreeViewItem" x:Key="TreeListViewItem">
<StackPanel>
<Border x:Name="Border">
<StackPanel Orientation="Horizontal">
<Border Background="White" Width="10" Height="10" Visibility="{Binding BtnVisible}" BorderBrush="DarkGray" BorderThickness="1">
<TextBlock x:Name="expander" Text="+" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" FontSize="10"/>
</Border>
<GridViewRowPresenter Content="{TemplateBinding Header}" Columns="{Binding Columns, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=com:TreeListView}}"/>
</StackPanel>
</Border>
<ItemsPresenter x:Name="ItemsPresenter" Visibility="Collapsed" Margin="0,2,0,0"/>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Setter TargetName="ItemsPresenter" Property="Visibility" Value="Visible"/>
<Setter TargetName="expander" Property="Text" Value="-"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter TargetName="Border" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style TargetType="{x:Type com:TreeListView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type com:TreeListView}">
<ControlTemplate.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="Margin" Value="5,1"/>
<Setter Property="Template" Value="{StaticResource TreeListViewItem}"/>
</Style>
</ControlTemplate.Resources>
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<GridViewHeaderRowPresenter Columns="{TemplateBinding Columns}"/>
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" Grid.Row="1">
<ItemsPresenter/>
</ScrollViewer>
</Grid>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
數據類
public class dataNode
{
public string Name { get; set; }
public string ID { get; set; }
public bool IsCheck { get; set; }
public List<dataNode> Children { get; set; }
public Thickness NameMargin
{
get { return Children != null ? new Thickness(0) : new Thickness(20, 0, 0, 0); }
}
public Visibility BtnVisible
{
get { return Children != null ? Visibility.Visible : Visibility.Collapsed; }
}
public Visibility CheckVisible
{
get { return Children != null ? Visibility.Collapsed : Visibility.Visible; }
}
}