Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 来自单个类的TreeView和HieracrhicalDataTemplate_Wpf_Treeview - Fatal编程技术网

Wpf 来自单个类的TreeView和HieracrhicalDataTemplate

Wpf 来自单个类的TreeView和HieracrhicalDataTemplate,wpf,treeview,Wpf,Treeview,我有一门课: class WorkOrder { public string Building {get; set;} public string Area {get; set;} public string Name {get; set;} List<Item> Items {get; set;} } 使用如下xaml: -Name -Item -Item -Name -Item -Item <Hierarc

我有一门课:

class WorkOrder
{
    public string Building {get; set;}
    public string Area {get; set;}
    public string Name {get; set;}
    List<Item> Items {get; set;}
}
使用如下xaml:

-Name
    -Item
    -Item
-Name
    -Item
    -Item
<HierarchicalDataTemplate DataType="{x:Type WorkOrder}" ItemsSource="{Binding Items}" >
    <TextBlock Text="{Binding Path=Name} Margin="5,0" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type Item}">
    <TextBlock Text="{Binding Path=Name}" />
</DataTemplate>

不确定我是应该看多重绑定还是其他地方。谢谢。

我想不出用绑定或XAML做这件事的任何方法。我有一个游戏,假设你不想更改你的工作顺序或项目类(因为如果你想/被允许更改它们,你可以在对象层次结构的另一个层中创建)

作为UI端解决方案,您可以执行以下操作:

1) 添加保存建筑分组信息的类

public class BuildingGroup
{
    public string Name { get; set; }
    public IEnumerable<WorkOrder> Orders { get; set; }
}
公共类构建组
{
公共字符串名称{get;set;}
公共IEnumerable命令{get;set;}
}
2) 创建转换器以将当前工单集合转换为建筑组集合

public class GroupingConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var workOrders = (IEnumerable<WorkOrder>)value;

        return GroupTheseBadBoysByBuilding(workOrders);
    }

    private IEnumerable GroupTheseBadBoysByBuilding(IEnumerable<WorkOrder> workOrders)
    {
        var buildingGroups = workOrders.GroupBy(w => w.Building);

        foreach (var buildingGroup in buildingGroups)
        {
            yield return new BuildingGroup
            {
                Name = buildingGroup.Key,
                Orders = buildingGroup,
            };
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
公共类分组转换器:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
var workOrders=(IEnumerable)值;
返回组Badboysbuilding(工作订单);
}
私人IEnumerable组Badboysbuilding(IEnumerable工单)
{
var buildingGroups=workOrders.GroupBy(w=>w.Building);
foreach(buildingGroup中的var buildingGroup)
{
新建筑群收益率
{
Name=buildingGroup.Key,
Orders=buildingGroup,
};
}
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
3) 为建筑组设置数据样板

    <HierarchicalDataTemplate DataType="{x:Type local:BuildingGroup}" ItemsSource="{Binding Orders}" >
        <TextBlock Text="{Binding Path=Name}" Margin="5,0" />
    </HierarchicalDataTemplate>

4) 创建转换器的实例并在树视图上使用它

<TreeView ItemsSource="{Binding Path=Orders,Converter={StaticResource testConverter}}" />


我认为这种方法唯一需要注意的是,您将丢失任何INotifyCollectionChanged处理(不知道您是否正在使用它!)

谢谢您的帮助。我没有使用INotifyCollectionChanged,绑定是一次性的。
<TreeView ItemsSource="{Binding Path=Orders,Converter={StaticResource testConverter}}" />