【问题标题】:Caliburn.Micro TreeView Bind to an class containing a list of another classCaliburn.Micro TreeView 绑定到包含另一个类列表的类
【发布时间】:2020-04-18 12:24:55
【问题描述】:

早安,

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

在我拥有的 UserViewModel 中

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

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>

我怎样才能让它工作

【问题讨论】:

    标签: treeview caliburn.micro


    【解决方案1】:

    您需要为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>
    

    【讨论】:

    • 谢谢你的回答,但我这里有一个错误 DataType="{x:Type vm:ProfileModel}" (命名空间前缀'vm'未定义)
    • 我添加了 xmlns:vm="clr-namespace:UserLibrary;assembly=UserLibrary" 现在它似乎可以工作了。再次感谢
    • 问题,如何在我的 ViewModel 中获取选中的项目?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    相关资源
    最近更新 更多