这可能会更容易Style
,ListView
我玩得很快,并设法在大约 10 分钟内创建了一个模型。
显然,您会想要添加更多内容,但TreeView
在ListView
列内托管似乎很容易。
如果您想用于构建,这里是模型代码。
xml:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="233" Width="405" Name="UI">
<Grid DataContext="{Binding ElementName=UI}">
<ListView ItemsSource="{Binding Processes}" >
<ListView.View>
<GridView>
<GridViewColumn Header="Process" Width="200" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TreeView BorderThickness="0" ItemsSource="{Binding Processes}" >
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Processes}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="CPU" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding CpuUsage, StringFormat={}{0} %}" TextAlignment="Right" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Memory" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding MemUsage, StringFormat={}{0} MB}" TextAlignment="Right" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="VerticalContentAlignment" Value="Top" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
</Window>
代码:对可树模型数据加载感到抱歉:)
public partial class MainWindow : Window
{
private ObservableCollection<ProcessInfo> _processes = new ObservableCollection<ProcessInfo>();
public MainWindow()
{
InitializeComponent();
Processes.Add(new ProcessInfo
{
CpuUsage = 10.3,
MemUsage = 48.9,
Processes = new ObservableCollection<Process>()
});
var pro = new Process{ Name = "Process1", Processes = new ObservableCollection<Process>()};
pro.Processes.Add(new Process { Name = "SubProcess1", Processes = new ObservableCollection<Process>() });
Processes[0].Processes.Add(pro);
Processes.Add(new ProcessInfo
{
CpuUsage = 0,
MemUsage = 100,
Processes = new ObservableCollection<Process>()
});
var pro2 = new Process { Name = "Process2", Processes = new ObservableCollection<Process>() };
pro2.Processes.Add(new Process { Name = "SubProcess1", Processes = new ObservableCollection<Process>() });
pro2.Processes.Add(new Process { Name = "SubProcess2", Processes = new ObservableCollection<Process>() });
pro2.Processes.Add(new Process { Name = "SubProcess3", Processes = new ObservableCollection<Process>() });
Processes[1].Processes.Add(pro2);
}
public ObservableCollection<ProcessInfo> Processes
{
get { return _processes; }
set { _processes = value; }
}
}
public class ProcessInfo
{
public ObservableCollection<Process> Processes { get; set; }
public double CpuUsage { get; set; }
public double MemUsage { get; set; }
}
public class Process
{
public string Name { get; set; }
public ObservableCollection<Process> Processes { get; set; }
}
祝你好运 :)