使用HierarchicalDataTemplate递归绑定现实
XAML代码:
<UserControl x:Class="SunCreate.CombatPlatform.Client.MultiSelOrgTree" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:ui="clr-namespace:SunCreate.CombatPlatform.Client" mc:Ignorable="d" d:DesignHeight="25" d:DesignWidth="200"> <Grid> <Button x:Name="btnSelected" Click="BtnClick" Height="25"> <Button.Template> <ControlTemplate> <Border Height="{TemplateBinding Property=Height}"> <Border.Background> <ImageBrush ImageSource="/SunCreate.CombatPlatform.Client.Resources;component/Image/Face/Enter.png"/> </Border.Background> <TextBlock Margin="3 0 0 0" Text="{TemplateBinding Property=Tag}" Foreground="#1ba4f6" VerticalAlignment="Center"></TextBlock> </Border> </ControlTemplate> </Button.Template> </Button> <Popup x:Name="popup" StaysOpen="False" PopupAnimation="Scroll" Width="280" Height="300" AllowsTransparency="True"> <Border Background="#00234E" BorderThickness="1" BorderBrush="#224066"> <TreeView x:Name="orgTree" > <TreeView.Template> <ControlTemplate> <ScrollViewer HorizontalScrollBarVisibility="Auto" MinHeight="{Binding ElementName=orgTree,Path=ActualHeight}" MinWidth="{Binding ElementName=orgTree, Path=ActualWidth}"> <ItemsPresenter></ItemsPresenter> </ScrollViewer> </ControlTemplate> </TreeView.Template> <TreeView.ItemTemplate> <HierarchicalDataTemplate DataType="{x:Type ui:MultiSelOrgTreeItemModel}" ItemsSource="{Binding Path=Nodes}" > <Border Height="25" Width="200"> <Grid VerticalAlignment="Center" HorizontalAlignment="Left"> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"></ColumnDefinition> <ColumnDefinition Width="15"></ColumnDefinition> <ColumnDefinition ></ColumnDefinition> </Grid.ColumnDefinitions> <CheckBox Visibility="{Binding Path=CheckVisiable}" Tag="{Binding}" IsChecked="{Binding Path=IsChecked, Mode=TwoWay}" Click="cxb_Node_Click" Checked="cxb_Node_Checked" Unchecked="cxb_Node_UnChecked"></CheckBox> <Image Grid.Column="1" Width="10" Height="10" Source="/SunCreate.CombatPlatform.Client.Resources;component/Image/orgIcon.png"></Image> <TextBlock Grid.Column="2" FontSize="12" VerticalAlignment="Center" Margin="10,0,0,0" x:Name="Name" Foreground="White" Text="{Binding Path=Name}"></TextBlock> </Grid> </Border> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> </Border> </Popup> </Grid> </UserControl>