如何在TreeView分层数据模板中添加其他控件?

时间:2018-04-04 12:39:43

标签: c# wpf xaml mvvm treeview

我有一个自定义的TreeView,如下所示:

----------------------------
|  ----------------------  |  
|  |                    |  |         
|  ----------------------  |         
|  ----------------------  |                            
|  |                    |  |           
|  ----------------------  |                             
|  ----------------------  |                           
|  |                    |  |
|  ----------------------  |
|                          |
---------------------------

---------------------------
|                         |
---------------------------

' - '是边界的边界。我已经删除了>扩展TreeView。另外,我已经做到了,所以TreeView总是被扩展。

父结构将托管边框内的所有TreeView项目,并且每个TreeView项目也将获得自己的边框。

我正在使用其他两个类继承的基础Team类。这两个继承的类使用HierarchicalDataTemplate来填充TreeView项。

我想做什么:

我的团队课程如下所示:

public abstract class Team
{
  public string Name { get; set; }
  public string Division { get; set;
}

我想要做的是将Division属性放在TreeView中,使它看起来像:

----------------------------
|  ----------------------  |  
|  |                    |  |         
|  ----------------------  | 
|   first blocks Division  | 
|  ----------------------  |                            
|  |                    |  |           
|  ----------------------  |
|   second blocks Division |                             
|  ----------------------  |                           
|  |                    |  |
|  ----------------------  |
|  third blocks Division   |
|                          |
---------------------------
  main big Blocks Division

---------------------------
|                         |
---------------------------
 smaller second Block's Division

因此,Division属性将位于TreeView的边界之外。

这是继承自团队

的两个类

DoubleA:

public class DoubleA : Team
{
  public DoubleA()
  {
     this.Players = new ObservableCollection<string>();
  }
 public ObservableCollection<string> Players { get; set; }
 }
}

SingleA:

public class SingleA : Team
{
  public SingleA()
  {
    this.Affiliations = new ObservableCollection<Team>();
  }
  public ObservableCollection<Team> Affiliations { get; set; }

在我的主视图模型中,我使用Organization类来填充我的数据:

public class Organization
{
  public Organization()
  {
    this.TopSingleA = new SingleA();
  }
  public SingleA TopSingleA { get; set; }
 }
}

这是我的主视图模型中填充TreeView的方法:

public class MainViewModel : NotifyPropertyChanged
{
  public MainViewModel()
  {
    this.Organization = new Organization { Name = "Major League Baseball "};
    this.PopulateView();
  }

 private Organization organization;
 public Organization Organization 
 {
    get
    {
      return this.organization;
    }
    set
    {
      this.organization = value;
      OnPropertyChanged("Organization");
    }
 }

public void PopulateView()
{
  SingleA advanced = new SingleA { Name = "Advanced A" };
  SingleA rookieLeague = new SingleA { Name = "Rookie League" };

  DoubleA player1 = new DoubleA { Name = "Tim Tebow" }; rookieLeague.Affiliations.Add(player1);
  DoubleA player2 = new DoubleA { Name = "Michael Jordan" }; rookieLeague.Affiliations.Add(player2);
  DoubleA player3 = new DoubleA { Name = "Bo Jackson" }; rookieLeague.Affiliations.Add(player3);

  DoubleA player4 = new DoubleA { Name = "Russell Wilson" };
  advanced.Affiliations.add(rookieLeague);
  advanced.Affiliations.add(player4);

  this.Organization.TopSingleA = advanced;
 }
}

这是我对TreeView的相关XAML:

<Window.Resources>
  <Style x:Key="TreeViewItemFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border>
                        <Rectangle Margin="0,0,0,0"
         StrokeThickness="5"
         Stroke="Black"
         StrokeDashArray="1 2"
         Opacity="0"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="IsExpanded" Value="True"/>
        <Setter Property="Margin" Value="30, 30, 200, 30"/>
        <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="Padding" Value="1,0,0,0"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TreeViewItem}">
                    <Border BorderBrush="Black" BorderThickness="1">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="10"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Border Name="Bd"
                          Grid.Column="1"                                
                          Background="{TemplateBinding Background}"
                          BorderBrush="{TemplateBinding BorderBrush}"
                          BorderThickness="{TemplateBinding BorderThickness}"
                          Padding="{TemplateBinding Padding}">
                                <ContentPresenter x:Name="PART_Header"
                                    ContentSource="Header"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                            </Border>
                            <ItemsPresenter x:Name="ItemsHost"
                      Grid.Row="1"
                      Grid.Column="1"
                      Grid.ColumnSpan="2"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsExpanded" Value="false">
                            <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="HasHeader" Value="false"/>
                                <Condition Property="Width" Value="Auto"/>
                            </MultiTrigger.Conditions>
                            <Setter TargetName="PART_Header" Property="MinWidth" Value="75"/>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="HasHeader" Value="false"/>
                                <Condition Property="Height" Value="Auto"/>
                            </MultiTrigger.Conditions>
                            <Setter TargetName="PART_Header" Property="MinHeight" Value="19"/>
                        </MultiTrigger>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="Bd" 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="Bd" 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>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid>
    <TreeView ItemsSource="{Binding Organization.TopSingleA.Affiliations}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:DoubleA}" ItemsSource="{Binding Path=Players}">
                <StackPanel>
                    <StackPanel Style="{StaticResource BaseStackPanelStyling}" VerticalAlignment="Center" Height="45">
                        <CheckBox Margin="0, 0, 10, 0"/>
                        <TextBlock Style="{StaticResource PlayerNameStyling}"/>
                        <TextBlock Style="{StaticResource TextBlockStyling}"/>
                    </StackPanel>
                </StackPanel>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:SingleA}" ItemsSource="{Binding Path=Affiliations}">
                <StackPanel>
                    <StackPanel Style="{StaticResource BaseStackPanelStyling}">
                        <CheckBox Margin="0, 0, 10, 0"/>
                            <TextBlock Style="{StaticResource LeagueNameStyling}"/>
                        <TextBlock Style="{StaticResource TextBlockStyling}"/>
                    </StackPanel>
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
</Grid>

0 个答案:

没有答案