• WPF treeview 多层次绑定问题 HierarchicalDataTemplate 和 CompositeCollection


     1     public class Clause : DependencyObject
     2     {
     3         public Clause()
     4         {
     5             this.Parent = null;
     6         }
     7         public Clause(Clause Parent)
     8         {
     9             this.Parent = Parent;
    10         }
    11         private ObservableCollection<Criteria> _criterias = new ObservableCollection<Criteria>();
    12         private ObservableCollection<Clause> _clauses = new ObservableCollection<Clause>();
    13 
    14         public Clause Parent { get; set; }
    15         public string Type { get; set; }
    16 
    17         public ObservableCollection<Criteria> Criterias
    18         {
    19             get { return _criterias; }
    20             set { _criterias = value; }
    21         }
    22         public ObservableCollection<Clause> Clauses
    23         {
    24             get { return _clauses; }
    25             set { _clauses = value; }
    26         }
    27 
    28         public CompositeCollection Children
    29         {
    30             get
    31             {
    32                 CompositeCollection o = new CompositeCollection();
    33                 o.Add(new CollectionContainer() { Collection = Clauses });
    34                 o.Add(new CollectionContainer() { Collection = Criterias });
    35                 return o;
    36             }
    37         }
    38 
    39     }
    40 
    41     public class Criteria : DependencyObject
    42     {
    43 
    44         public Criteria(Clause Parent,
    45                         string Category,
    46                         string Field,
    47                         string Operator,
    48                         string Value)
    49         {
    50             this.Parent = Parent;
    51             this.Category = Category;
    52             this.Field = Field;
    53             this.Operator = Operator;
    54             this.Value = Value;
    55         }
    56 
    57         public Clause Parent { get; set; }
    58         public string Category { get; set; }
    59         public string Field { get; set; }
    60         public string Operator { get; set; }
    61         public string Value { get; set; }
    62 
    63     }
      1         <TreeView Name="ruleTreeView"
      2                   ItemsSource="{Binding Clauses}">
      3             <TreeView.Resources>
      4                 <Style TargetType="TreeViewItem">
      5                     <Setter Property="Template">
      6                         <Setter.Value>
      7                             <ControlTemplate TargetType="TreeViewItem">
      8                                 <Grid Margin="2,2,0,2"
      9                                       ShowGridLines="False">
     10                                     <Grid.RowDefinitions>
     11                                         <!--The top row contains the item's content.-->
     12                                         <RowDefinition Height="Auto" />
     13                                         <!--The bottom row contains the item's children.-->
     14                                         <RowDefinition Height="*" />
     15                                     </Grid.RowDefinitions>
     16                                     <Grid.ColumnDefinitions>
     17                                         <ColumnDefinition Width="30" />
     18                                         <ColumnDefinition Width="*" />
     19                                     </Grid.ColumnDefinitions>
     20 
     21                                     <ContentPresenter Grid.Row="0"
     22                                                       Grid.ColumnSpan="2"
     23                                                       Name="PART_Header"
     24                                                       ContentSource="Header" />
     25 
     26                                     <Border Grid.Row="1"
     27                                             Grid.Column="0"
     28                                             BorderBrush="#FF7F9DB9"
     29                                             BorderThickness="1,0,0,0" />
     30 
     31                                     <ItemsPresenter Grid.Row="1"
     32                                                     Grid.Column="1"
     33                                                     Margin="0,0,0,0" />
     34 
     35                                 </Grid>
     36                             </ControlTemplate>
     37                         </Setter.Value>
     38                     </Setter>
     39                 </Style>
     40                 <HierarchicalDataTemplate DataType="{x:Type local:Clause}"
     41                                           ItemsSource="{Binding Children}">
     42                     <DockPanel>
     43                         <!--<ComboBox ItemsSource="{StaticResource ClauseListString}"
     44                                   DisplayMemberPath="DisplayString"
     45                                   SelectedValuePath="ValueString"
     46                                   SelectedValue="{Binding Type}"
     47                                   Width="50"
     48                                   DockPanel.Dock="Left" />-->
     49                         <TextBlock Text="{Binding Type}"></TextBlock>
     50 
     51                         <Label DockPanel.Dock="Left"
     52                                HorizontalAlignment="Left">of the following are met</Label>
     53                         <Button DockPanel.Dock="Right"
     54                                 HorizontalAlignment="Right"
     55                                 Width="20"
     56                                 Click="DelButton_Click"
     57                                 Tag="{Binding}"
     58                                 x:Name="DelButton">-</Button>
     59                         <Button DockPanel.Dock="Right"
     60                                 HorizontalAlignment="Right"
     61                                 Width="20"
     62                                 Click="AddButton_Click"
     63                                 Tag="{Binding}">+</Button>
     64                         <Button DockPanel.Dock="Right"
     65                                 HorizontalAlignment="Right"
     66                                 Width="20"
     67                                 Click="AddChildButton_Click"
     68                                 Tag="{Binding}"></Button>
     69                     </DockPanel>
     70 
     71                 </HierarchicalDataTemplate>
     72                 <DataTemplate DataType="{x:Type local:Criteria}">
     73                     <Grid Margin="0">
     74                         <Grid.ColumnDefinitions>
     75                             <ColumnDefinition Width="Auto" />
     76                             <ColumnDefinition Width="Auto" />
     77                             <ColumnDefinition Width="150" />
     78                             <ColumnDefinition Width="100" />
     79                             <ColumnDefinition Width="200" />
     80                             <ColumnDefinition Width="*" />
     81                         </Grid.ColumnDefinitions>
     82                         <Grid.RowDefinitions>
     83                             <RowDefinition Height="Auto" />
     84                         </Grid.RowDefinitions>
     85 
     86                         <Button Grid.Column="0"
     87                                 Click="OutdentButton_Click"
     88                                 x:Name="OutdentButton"
     89                                 Tag="{Binding}">
     90                             <Canvas Width="5"
     91                                     Height="6"
     92                                     Canvas.Left="0"
     93                                     Canvas.Top="0">
     94                                 <Path Width="5"
     95                                       Height="6"
     96                                       Stretch="Fill"
     97                                       Fill="#FF000000"
     98                                       Data="F1 M 610.68,232.403L 592.412,214.134C 591.77,213.492 590.807,213.3 589.968,213.648C 589.131,213.995 588.584,214.812 588.584,215.72L 588.584,252.257C 588.584,253.164 589.131,253.981 589.968,254.328C 590.247,254.444 590.538,254.498 590.826,254.498C 591.41,254.498 591.983,254.272 592.412,253.844L 610.68,235.573C 611.098,235.156 611.338,234.578 611.338,233.988C 611.338,233.398 611.098,232.82 610.68,232.403 Z "
     99                                       RenderTransformOrigin="0.5,0.5">
    100                                     <Path.RenderTransform>
    101                                         <RotateTransform Angle="180" />
    102                                     </Path.RenderTransform>
    103                                 </Path>
    104                             </Canvas>
    105                         </Button>
    106                         <Button Grid.Column="1"
    107                                 Click="IndentButton_Click"
    108                                 x:Name="IndentButton"
    109                                 Tag="{Binding}">
    110                             <Canvas Width="5"
    111                                     Height="6"
    112                                     Canvas.Left="0"
    113                                     Canvas.Top="0">
    114                                 <Path Width="5"
    115                                       Height="6"
    116                                       Stretch="Fill"
    117                                       Fill="#FF000000"
    118                                       Data="F1 M 610.68,232.403L 592.412,214.134C 591.77,213.492 590.807,213.3 589.968,213.648C 589.131,213.995 588.584,214.812 588.584,215.72L 588.584,252.257C 588.584,253.164 589.131,253.981 589.968,254.328C 590.247,254.444 590.538,254.498 590.826,254.498C 591.41,254.498 591.983,254.272 592.412,253.844L 610.68,235.573C 611.098,235.156 611.338,234.578 611.338,233.988C 611.338,233.398 611.098,232.82 610.68,232.403 Z " />
    119                             </Canvas>
    120                         </Button>
    121 
    122                         <ComboBox Grid.Column="2"
    123                                   ItemsSource="{StaticResource FieldListString}"
    124                                   DisplayMemberPath="ValueString"
    125                                   SelectedValuePath="ValueString"
    126                                   SelectedValue="{Binding Field, Mode=TwoWay}" />
    127                         <ComboBox Grid.Column="3"
    128                                   ItemsSource="{StaticResource OperatorListString}"
    129                                   DisplayMemberPath="ValueString"
    130                                   SelectedValuePath="ValueString"
    131                                   SelectedValue="{Binding Operator, Mode=TwoWay}" />
    132                         <TextBox Grid.Column="4"
    133                                  Text="{Binding Value, Mode=TwoWay}"
    134                                  x:Name="ValueTextBox" />
    135                         <ComboBox Grid.Column="4"
    136                                   ItemsSource="{StaticResource GroupListString}"
    137                                   DisplayMemberPath="ValueString"
    138                                   SelectedValuePath="ValueString"
    139                                   SelectedValue="{Binding Value, Mode=OneWay}"
    140                                   x:Name="ValueCombo"
    141                                   Visibility="Collapsed" />
    142                         <DockPanel Grid.Column="5">
    143                             <Button DockPanel.Dock="Right"
    144                                     HorizontalAlignment="Right"
    145                                     Width="20"
    146                                     Click="DelButton_Click"
    147                                     Tag="{Binding}">-</Button>
    148                             <Button DockPanel.Dock="Right"
    149                                     HorizontalAlignment="Right"
    150                                     Width="20"
    151                                     Click="AddButton_Click"
    152                                     Tag="{Binding}">+</Button>
    153                             <Button DockPanel.Dock="Right"
    154                                     HorizontalAlignment="Right"
    155                                     Width="20"
    156                                     Click="AddChildButton_Click"
    157                                     Tag="{Binding}"></Button>
    158 
    159                         </DockPanel>
    160                     </Grid>
    161 
    162                 </DataTemplate>
    163 
    164             </TreeView.Resources>
    165 
    166         </TreeView>
  • 相关阅读:
    杂记 后台代码取DataSource中的值,和不间断滚动JS
    利用IHttpModule实现URL地址转发功能
    在ASP.NET中跟踪和恢复大文件下载
    小记,取GB2312汉字的首字母
    小记,提供文件下载,并控制下载速度
    黑客知识系列之木马程序隐身的技术
    经典算法C#四种排序算法
    跟我学做c#皮肤美化(六)
    【转】xPath语法介绍
    一个httpwebrequest异步下载的例子
  • 原文地址:https://www.cnblogs.com/movingcity/p/2679050.html
Copyright © 2020-2023  润新知