c# - 带有 Caliburn Micro wpf 的多个模板 TreeView

标签 c# wpf mvvm treeview caliburn.micro

我的 TreeView 有问题。我正在尝试使用它的三层结构,但是当我将 hiearhical 模板放在根节点中时,即使它包含子节点,它也不会显示展开按钮或允许我展开它。

我围绕这篇文章制作了模板 TreeView Multiple Templates

所以我的 xaml 看起来像这样:

<TreeView Grid.Row="1" Grid.Column="1" MinWidth="150"
                          ItemsSource="{Binding Operations}" Name="Operations">
                    <TreeView.Resources>
                        <HierarchicalDataTemplate DataType="{x:Type local:Operation}" ItemsSource="{Binding Operation}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Name}" />
                                <TextBlock Text=" " ></TextBlock>
                                <TextBlock Text="{Binding Parameters.Count}" />
                            </StackPanel>
                        </HierarchicalDataTemplate>
                        <HierarchicalDataTemplate DataType="{x:Type local:Parameters}" ItemsSource="{Binding Parameters}">
                            <StackPanel>
                                <TextBlock Text="{Binding Path=par}" ToolTip="{Binding Path=Path}" />
                                <TextBlock Text="{Binding Path=val}" ToolTip="{Binding Path=Path}" />
                            </StackPanel>
                        </HierarchicalDataTemplate>
                        <HierarchicalDataTemplate DataType="{x:Type local:Joints}" ItemsSource="{Binding Joints}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Par}" />
                                <TextBlock Text=" " ></TextBlock>
                                <TextBlock Text="{Binding Val}" />
                            </StackPanel>
                        </HierarchicalDataTemplate>
                    </TreeView.Resources>
                    <TreeView.ItemContainerStyle>
                        <Style TargetType="TreeViewItem">
                            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                        </Style>
                    </TreeView.ItemContainerStyle>
                </TreeView>

我有单独的操作、参数和关节类,如下所示:

操作:
    public class Operation
{
    public string Name { get; set; }
    public int Id { get; set; }

    public ObservableCollection<Parameters> Parameters { get; set; }

    public Operation()
    {
        Parameters = new ObservableCollection<Parameters>();
    }
}

参数:
    public class Parameters
{
    public string par { get; set; }
    public double val { get; set; }

    public ObservableCollection<Joints> Joints { get; set; }

    public Parameters()
    {
        Joints = new ObservableCollection<Joints>();
    }
}

关节:
    public class Joints
{
    public string Par { get; set; }
    public double Val { get; set; }
}

我的 ShellViewModel 实现了所有这些类,如下所示:
public BindableCollection<Operation> _Operations = new BindableCollection<Operation>();


private bool _IsExpanded;
public bool IsExpanded
{
    get 
    { return _IsExpanded; }
    set 
    { 
        _IsExpanded = value;
        NotifyOfPropertyChange(() => IsExpanded);
    }
}
private bool _IsSelected;
public bool IsSelected
{
    get { return _IsSelected; }
    set 
    { 
        _IsSelected = value;
        NotifyOfPropertyChange(() => IsSelected);
    }
}

public Operation _Operation = new Operation();
int moveId = 0;

public BindableCollection<Operation> Operations
{
    get { return _Operations; }
    set 
    { 
        _Operations = value;
        NotifyOfPropertyChange(() => Operations);
    }
}

public ShellViewModel()
{

var moveOperation = new Operation
    {
        Id = moveId,
        Name = $"Move{moveId}"
    };
    Joints jt1 = new Joints() { Par = "J0", Val = 2.34 };
    Joints jt2 = new Joints() { Par = "J1", Val = 0.34 };
    Joints jt3 = new Joints() { Par = "J2", Val = 4.34 };
    Parameters parm = new Parameters { par = "a", val = 2.13 };
    parm.Joints.Add(jt1);
    parm.Joints.Add(jt2);
    parm.Joints.Add(jt3);
    moveOperation.Parameters.Add(parm);
    _Operations.Add(moveOperation);

预期的结果是这样的:
 - Move1
    -a 2.13
    -Joints
      -J0 2.34
      -J1 0.34
      -J2 4.34
 - Move2
    -a 'some value'
    -Joints
      -J0 'some value'
      -J1 'some value'
      -J2 'some value'

我错过了什么?为什么我只能看到我的根\父\操作节点?我无法扩展它。

最佳答案

<HierarchicalDataTemplate DataType="{x:Type local:Operation}" ItemsSource="{Binding Operation}">
应该更新为
<HierarchicalDataTemplate DataType="{x:Type local:Operation}" ItemsSource="{Binding Operations}">
这会将集合正确地绑定(bind)到 TopLevelNode 和随后的模板。
这是 Github Demo

关于c# - 带有 Caliburn Micro wpf 的多个模板 TreeView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59867526/

相关文章:

c# - 文本方向

c# - Winrt Xaml Grid ROw 没有用 * 填充空间

c# - Wpf中的中继命令

c# - 在事件上使用 GUI 功能 (MVVM)

c# - 什么时候应该避免扩展方法?

c# - WPF 运行时区域设置更改,重新评估 ValueConverters UI

c# - Web.Config 中的 Oracle.ManagedDataAccess TNS_ADMIN

c# - WPF 数据模板和自动创建的 viewModel 对象

C# 运行时类创建

c# - 将 IOrderedEnumerable 转换为 ICollection