zoukankan      html  css  js  c++  java
  • 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 + Grid

    wpf

    <!--直接绑定单个属性     prop   ObservableCollection<BitmapImage> EntityObj-->
    <HierarchicalDataTemplate x:Key="ListItem" ItemsSource="{Binding}">
        <Grid Background="Transparent">
            <Image Source="{Binding}"/>
        </Grid>
    </HierarchicalDataTemplate>
    
    
    <!--绑定多个属性的对象     prop   ObservableCollection<EntityDto> EntityObj-->
    <HierarchicalDataTemplate x:Key="ListItem" ItemsSource="{Binding}">
        <Grid Background="Transparent">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinitin 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>
    

    Prism

    分层数据样式的模板也支持自定义控件的属性依赖绑定。

    <UserControl.Resources>
        <HierarchicalDataTemplate x:Key="ListItem" ItemsSource="{Binding}">
            <Grid Background="Transparent">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" Margin="5" Text="{Binding DisplayName}"/>
                <comm:ProgressBarEx Grid.Row="1" PercentageVisible="False"
                                    Value="{Binding DataContext.DisplayValue, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBoxItem}}}"/>
            </Grid>
        </HierarchicalDataTemplate>
    </UserControl.Resources>
    

    demo

    <ListBox ItemsSource="{Binding EntityObj}" ItemTemplate="{DynamicResource ListItem}"
             Width="{Binding Width, ElementName=GridMain}" ScrollViewer.VerticalScrollBarVisibility="Auto" FocusVisualStyle="{x:Null}" BorderThickness="0">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="3"/> <!--每行3列-->
                <StackPanel Orientation="Vertical"/> <!--每行1列-->
                <!--<WrapPanel IsItemsHost="True"/> 列数随宽度变化-->
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
    

    如果想隐藏ListBox的样式,比如选择项的背景色,可以这样设置:

        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <ContentPresenter/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    

    ListBox + RadioButton

    wpf

    <HierarchicalDataTemplate x:Key="ListItem" ItemsSource="{Binding}">
       <RadioButton x:Name="radioBtn" FontSize="14" GroupName="area">
           <RadioButton.IsChecked>
               <Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType=ListBoxItem}" Mode="TwoWay" />
           </RadioButton.IsChecked>
           <RadioButton.Content>
               <Binding Path="Content" RelativeSource="{RelativeSource AncestorType=ListBoxItem}" Mode="TwoWay" />
           </RadioButton.Content>
       </RadioButton>
    </HierarchicalDataTemplate>
    

    prism

    
    
    
    
  • 相关阅读:
    python 全栈开发,Day43(python全栈11期月考题)
    python 全栈开发,Day43(引子,协程介绍,Greenlet模块,Gevent模块,Gevent之同步与异步)
    python 全栈开发,Day42(Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures)
    python 全栈开发,Day41(线程概念,线程的特点,进程和线程的关系,线程和python 理论知识,线程的创建)
    python 全栈开发,Day40(进程间通信(队列和管道),进程间的数据共享Manager,进程池Pool)
    python 全栈开发,Day39(进程同步控制(锁,信号量,事件),进程间通信(队列,生产者消费者模型))
    python 全栈开发,Day38(在python程序中的进程操作,multiprocess.Process模块)
    python 全栈开发,Day37(操作系统的发展史)
    python 全栈开发,Day36(作业讲解(大文件下载以及进度条展示),socket的更多方法介绍,验证客户端链接的合法性hmac,socketserver)
    python 全栈开发,Day35(TCP协议 粘包现象 和解决方案)
  • 原文地址:https://www.cnblogs.com/wesson2019-blog/p/14917891.html
Copyright © 2011-2022 走看看