HierarchicalDataTemplate

分层数据模板,用来定义分层数据样式的模板,一般多用于TreeViewItem。

TreeViewItem

Wpf

<HierarchicalDataTemplate x:Key="ItemNode" ItemsSource="{Binding Children}">
    <Grid Background="Transparent">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="25"></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <CheckBox Grid.Column="0" IsChecked="{Binding IsCheck}" Tag="{Binding Node}" 
                  Checked="ItemNode_Checked" Unchecked="ItemNode_Unchecked">
        </CheckBox>
        <TextBlock Grid.Column="1" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="3 0" />
        <TextBlock Grid.Column="2" Text="{Binding DataVersionCacahe}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="3 0" />
    </Grid>
</HierarchicalDataTemplate>


Prism

<HierarchicalDataTemplate x:Key="ItemNode" ItemsSource="{Binding Children}">
    <Grid Background="Transparent">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="25"></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <CheckBox Grid.Column="0" IsChecked="{Binding IsCheck}" Tag="{Binding Node}" x:Name="TreeItemNode">
            <b:Interaction.Triggers>
                <b:EventTrigger EventName="Checked">
                    <b:InvokeCommandAction Command="{Binding DataContext.TreeItemNodeCheckedCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" 
                               CommandParameter="{Binding ElementName=TreeItemNode}"/>
                </b:EventTrigger>
                <b:EventTrigger EventName="Unchecked">
                    <b:InvokeCommandAction Command="{Binding DataContext.TreeItemNodeUncheckedCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" 
                               CommandParameter="{Binding ElementName=TreeItemNode}"/>
                </b:EventTrigger>
            </b:Interaction.Triggers>
        </CheckBox>
        <TextBlock Grid.Column="1" Text="{Binding Property1}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="3 0" />
        <TextBlock Grid.Column="2" Text="{Binding Property2}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="3 0" />
    </Grid>
</HierarchicalDataTemplate>

<TreeView Name="TreeView1" ItemsSource="{Binding TreeView1Source}" ItemTemplate="{DynamicResource ItemNode}"
BorderThickness="0" Style="{DynamicResource TreeViewExpanded}"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode ="Standard">

ListBox

wpf

<HierarchicalDataTemplate x:Key="ListItem" ItemsSource="{Binding}">
    <Grid Background="Transparent">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0">
            <Run Text="第"/>
            <Run Text="{Binding ImageIndex}" />
            <Run Text="张图:"/>
        </TextBlock>
        <ComboBox Grid.Column="1" SelectedValuePath="Key" DisplayMemberPath="Value" Width="100" 
                  ItemsSource="{Binding ComboBoxDataResource}" SelectedIndex="{Binding ComboBoxIndex}"/>
        <TextBox  Grid.Column="2" Width="50" Text="{Binding IntervalSencond}"/>
        <TextBlock Grid.Column="3" Margin="5" VerticalAlignment="Center" Text="s"/>
        <Button   Grid.Column="4" Content="X" Tag="{Binding Guid}" Click="ItemRemove_Click" Visibility="{Binding AddVisible}"
                  Width="32" Height="32" Style="{DynamicResource ButtonICOStyle}" HorizontalAlignment="Left" VerticalAlignment="Center">
            <Button.Background>
                <ImageBrush ImageSource="/My.Application;component/Image/Add.png"/>
            </Button.Background>
        </Button>
        <Button   Grid.Column="5" Content="+" Click="ItemAdd_Click" Visibility="{Binding RemoveVisible}"
                  Width="32" Height="32" Style="{DynamicResource ButtonICOStyle}" HorizontalAlignment="Left" VerticalAlignment="Center">
            <Button.Background>
                <ImageBrush ImageSource="/My.Application;component/Image/Delete.png"/>
            </Button.Background>
        </Button>
    </Grid>
</HierarchicalDataTemplate>

<ListBox ItemsSource="{Binding}" ItemTemplate="{DynamicResource ListItem}"
Width="{Binding Width, ElementName=GridMain}" ScrollViewer.VerticalScrollBarVisibility="Auto">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3"/>
<!-- 列数随宽度变化-->
</ItemsPanelTemplate>
</ListBox.ItemsPanel>


HierarchicalDataTemplate

上一篇:外键关联on_delete参数含义


下一篇:element ui 表格合并方法