我不是树视图专家,但是在没有树视图的情况下很容易构建类似的东西。
从一个名为 WpfTreeGridWhatever 的空 VS2008 Wpf 应用程序开始
首先,让我们定义我们的模型:
using System;
using System.Collections.Generic;
namespace WpfTreeGridWhatever
{
public class ItemBase
{
}
public class Group : ItemBase
{
public string Name { get; set; }
public IList<ItemBase> Items { get; set; }
}
public class Item : ItemBase
{
public string Name { get; set; }
public IList<Parameter> Parameters { get; set; }
}
public class Parameter
{
public string Name { get; set; }
public string Value { get; set; }
}
}
现在,在 Window1 构造函数中创建我们的对象(这样我们就有了一些要绑定的数据):
public Window1()
{
DataContext = new Group[]
{
new Group()
{
Name="Group A",
Items = new List<ItemBase>()
{
new Item()
{
Name="Item",
Parameters=new List<Parameter>()
{
new Parameter(){Name="Param 1",Value="12"},
new Parameter(){Name="Param 2",Value="true"},
new Parameter(){Name="Param 3",Value="0.0"},
new Parameter(){Name="Param 4",Value="off"},
}
},
new Item()
{
Name="Item",
Parameters=new List<Parameter>()
{
new Parameter(){Name="Param 1",Value="12"},
new Parameter(){Name="Param 2",Value="true"}
}
},
new Group()
{
Name="Group B",
Items = new List<ItemBase>()
{
new Item()
{
Name="Item",
Parameters=new List<Parameter>()
{
new Parameter(){Name="Param 1",Value="12"},
new Parameter(){Name="Param 2",Value="true"},
new Parameter(){Name="Param 3",Value="0.0"},
new Parameter(){Name="Param 4",Value="off"},
}
},
new Item()
{
Name="Item",
Parameters=new List<Parameter>()
{
new Parameter(){Name="Param 1",Value="12"},
new Parameter(){Name="Param 2",Value="true"},
new Parameter(){Name="Param 3",Value="0.0"},
new Parameter(){Name="Param 4",Value="off"},
new Parameter(){Name="Param 5",Value="2000"},
}
},
new Item()
{
Name="Item",
Parameters=new List<Parameter>()
{
new Parameter(){Name="Param 1",Value="12"},
new Parameter(){Name="Param 2",Value="true"},
}
},
new Group()
{
Name="Group C",
Items = new List<ItemBase>()
{
new Item()
{
Name="Item",
Parameters=new List<Parameter>()
{
new Parameter(){Name="Param 1",Value="12"},
new Parameter(){Name="Param 2",Value="true"},
new Parameter(){Name="Param 3",Value="0.0"},
new Parameter(){Name="Param 4",Value="off"},
}
},
new Item()
{
Name="Item",
Parameters=new List<Parameter>()
{
new Parameter(){Name="Param 1",Value="12"},
new Parameter(){Name="Param 2",Value="true"},
new Parameter(){Name="Param 3",Value="0.0"},
new Parameter(){Name="Param 4",Value="off"},
new Parameter(){Name="Param 5",Value="2000"},
}
},
new Item()
{
Name="Item",
Parameters=new List<Parameter>()
{
new Parameter(){Name="Param 1",Value="12"},
new Parameter(){Name="Param 2",Value="true"},
}
},
}
}
}
}
}
}
};
InitializeComponent();
}
现在,魔术 - 在 Window1.xaml 中使用此代码
<Window x:Class="WpfTreeGridWhatever.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:WpfTreeGridWhatever"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<LinearGradientBrush x:Key="Bk" StartPoint="0,0" EndPoint="0,1" >
<GradientStop Offset="0" Color="DarkGray"/>
<GradientStop Offset="1" Color="White"/>
</LinearGradientBrush>
<DataTemplate DataType="{x:Type l:Parameter}">
<Border CornerRadius="5" Background="{StaticResource Bk}"
BorderThickness="1" BorderBrush="Gray" Margin="2" >
<StackPanel Margin="5">
<TextBlock Height="12" Text="{Binding Name}"/>
<TextBox Height="22" Text="{Binding Value}"/>
</StackPanel>
</Border>
</DataTemplate>
<DataTemplate DataType="{x:Type l:Item}" >
<StackPanel>
<Border CornerRadius="5" Background="{StaticResource Bk}"
BorderThickness="1" BorderBrush="Gray" Height="25" Margin="3">
<TextBlock Height="12" Text="{Binding Name}" VerticalAlignment="Center" Margin="3,0"/>
</Border>
<ItemsControl ItemsSource="{Binding Parameters}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type l:Group}">
<StackPanel>
<Border CornerRadius="5" Background="{StaticResource Bk}"
BorderThickness="1" BorderBrush="Gray" Height="25" Margin="3">
<TextBlock Height="12" Text="{Binding Name}" VerticalAlignment="Center" Margin="3,0"/>
</Border>
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</Window>
这应该让你开始