You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WPF TreeView中显示对象多属性,仅子容器为层级项

解决方案

步骤1:修改NestedContainer类,添加合并子项的集合

在原类中新增一个只读属性,将childContainers、ActionData、Modifier合并成一个统一的集合,供TreeView绑定:

public class NestedContainer
{
    public FXR1.FXContainer myContainer { get; set; }
    public string XID { get { return myContainer.XID; } }
    public FXR1.FXActionData ActionData { get { return myContainer.ActionData; } }
    public FXR1.FXModifier Modifier { get { return myContainer.Modifier; } }
    public List<NestedContainer> childContainers { get; set; } = new List<NestedContainer>();

    // 合并子容器与附加信息的集合
    public IEnumerable<object> AllChildren
    {
        get
        {
            // 先返回所有子容器
            foreach (var child in childContainers)
                yield return child;
            
            // 存在则返回ActionData
            if (ActionData != null)
                yield return ActionData;
            
            // 存在则返回Modifier
            if (Modifier != null)
                yield return Modifier;
        }
    }
}

步骤2:更新XAML中的TreeView配置

修改HierarchicalDataTemplate的ItemsSource为新的AllChildren集合,并为FXActionData和FXModifier分别定义显示模板:

<TreeView x:Name="FXNodeTree" Grid.Row="1" Grid.Column="1" ItemsSource="{Binding}">
    <TreeView.Resources>
        <!-- 嵌套容器的模板,绑定合并后的所有子项 -->
        <HierarchicalDataTemplate DataType="{x:Type local:NestedContainer}" ItemsSource="{Binding Path=AllChildren}">
            <TextBlock Text="{Binding Path=XID}" />
        </HierarchicalDataTemplate>

        <!-- FXActionData的显示模板,根据实际属性调整内容 -->
        <DataTemplate DataType="{x:Type FXR1:FXActionData}">
            <TextBlock Text="Action Data: {Binding YourActionProperty}" />
        </DataTemplate>

        <!-- FXModifier的显示模板,根据实际属性调整内容 -->
        <DataTemplate DataType="{x:Type FXR1:FXModifier}">
            <TextBlock Text="Modifier: {Binding YourModifierProperty}" />
        </DataTemplate>
    </TreeView.Resources>
</TreeView>

注意事项

  • 模板中的YourActionProperty和YourModifierProperty需要替换为FXActionData和FXModifier实际存在的属性名称,比如如果FXActionData有Name属性,就写成{Binding Name}。
  • 如果ActionData或Modifier会动态变化,需要让NestedContainer实现INotifyPropertyChanged接口,并在这两个属性变化时触发AllChildren的属性变更通知,确保UI能实时更新:
    public event PropertyChangedEventHandler PropertyChanged;
    
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    
    // 假设myContainer是可设置的,修改时触发通知
    private FXR1.FXContainer _myContainer;
    public FXR1.FXContainer myContainer
    {
        get => _myContainer;
        set
        {
            _myContainer = value;
            OnPropertyChanged(nameof(XID));
            OnPropertyChanged(nameof(ActionData));
            OnPropertyChanged(nameof(Modifier));
            OnPropertyChanged(nameof(AllChildren)); // 通知AllChildren更新
        }
    }
    

内容的提问来源于stack exchange,提问作者Morgan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 04:20:28