Caliburn.Micro TreeView 绑定到一个 class 包含另一个 class 的列表

Caliburn.Micro TreeView Bind to an class containing a list of another class

早上好,

我是编程初学者(和法语!)并开始使用 caliburn.micro。 我有一个 class

在我的 UserViewModel 中

        public BindableCollection<ProfileModel> Profiles { get; set; }

class ProfileViewModel 包含

    public class ProfileModel
{
    public List<UserModel> Users { get; set; } = new List<UserModel>();
    public string ProfileName { get; set; }//12 Char

并且 UserModelClass 包含

   public class UserModel
{
    public string Name { get; set; }//40 char

现在我可以使用配置文件名称绑定到 TreeView,但我不知道如何将用户绑定为配置文件的子项

我的xaml

<UserControl x:Class="MainUI.Views.UserView"
         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:local="clr-namespace:MainUI.Views"
         mc:Ignorable="d"  
         d:DesignHeight="450" d:DesignWidth="800">
<Grid>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="20"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="20"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition Height="20"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="20"/>
    </Grid.RowDefinitions>
    <Menu Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="4" Margin="0">
        <MenuItem Header="_File">
            <MenuItem Header="_Open" Name="FileOpen"/>
        </MenuItem>
    </Menu>
    <Button x:Name="GetProfiles" Grid.Row="1" Grid.Column="1">Get Profiles</Button>
    <StackPanel Orientation="Vertical" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="1" Grid.RowSpan="3" MaxHeight="250">
        <TextBlock FontWeight="Bold" Margin="0 10 0 0">Existing Profiles</TextBlock>
        <TextBlock x:Name="SelectedProfile_ProfileName"/>
    </StackPanel>
    <StackPanel Orientation="Vertical" Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="1" Grid.RowSpan="3" MaxHeight="250">
        <TextBlock FontWeight="Bold" Margin="0 10 0 0">Users in profile</TextBlock>
        <ListBox x:Name="Users" DisplayMemberPath="Name"
                 SelectedItem="{Binding Path=SelectedUser,Mode=OneWayToSource}"
                 Height="200"/>
        <TextBlock x:Name="SelectedUser_Name"/>
    </StackPanel>
    <StackPanel Orientation="Vertical" Grid.Row="2" Grid.Column="3" Grid.ColumnSpan="2" Grid.RowSpan="3" MaxHeight="250">
        <TextBlock FontWeight="Bold" Margin="0 10 0 0">Profiles</TextBlock>
        <TreeView x:Name="TVUser"  ItemsSource="{Binding Profiles}">
            <TreeView.ItemContainerStyle>
                <Style TargetType="{x:Type TreeViewItem}">
                    <!--<EventSetter Event="UIElement.MouseLeftButtonUp" Handler="TreeViewItem_MouseLeftButtonUp"/>-->
                    <Setter Property="IsSelected" Value="{Binding IsChecked, Mode=TwoWay}" />
                </Style>
            </TreeView.ItemContainerStyle>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Profiles}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock VerticalAlignment="Center" Text="{Binding ProfileName}"/>
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </StackPanel>
</Grid>

我怎样才能让它发挥作用

您需要为 HierarchicalDataTemplate 设置 DataTypeItemSource。例如,

<TreeView ItemsSource="{Binding Profiles}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Users}" DataType="{x:Type vm:ProfileModel}">
            <TextBlock Text="{Binding ProfileName}"/>
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate DataType="{x:Type vm:UserModel}">
                    <TextBlock Text="{Binding Name}"/>
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>