使用Treeview和Observable集合的过程监视器 [英] Process Monitor Using Treeview and Observable Collection

查看:50
本文介绍了使用Treeview和Observable集合的过程监视器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在服务器上运行WPF窗口以显示正在发生的事情,现在我只需要启动&停止按钮,但实际操作的一些细节将非常不错.在调试过程中,我有很多消息框可以& 确实弹出时,将这些相同的消息泵入功能以更新状态xml会很简单.

My goal is to have a WPF window run at the server to display what's occuring, right now I just have a start & stop button, but some details of what's actually going on would be really nice. During my debug I have many message boxes that can & do pop up, it would be trivial to pump those same messages into a function to update the status xml.

我正在寻找样本或线索,我放弃了使用列表控制和制表符来模仿父级小树的树状视图,它的几个孩子.现在,我正在考虑其数据源为xml且可观察到的树视图 收集可以工作(?)

I'm seeking samples or clues, I gave up on using a list control & tab characters to similate a treeview of small trees of a parent & its few children. Now I'm thinking that an treeview whose datasource is xml whose source is an observable collection could work(?)

假设...

1. Treeview控件的顶"次数为XML作为数据源

1. Treeview control "likes" XML as a data source

2. Treeview可以响应可观察的集合,可以将可观察的集合转换为xml以便在树视图中轻松显示,或者有更好的方法.

2. Treeview can respond to an observable collection, can an observable collection be converted to xml for easy display in a treeview, or is there a better way.

例如,我想让我的treeview标准化的内容/文本显示出来,例如...

I would like to bubble up to my treeview standarized content/text of what's occuring, for example...

(父母)等待下一个任务2012-03-13 13:42.657 ..."

(parent) "Waiting for next task 2012-03-13 13:42.657..."

(父母)"Begin GetSettings 2012-03-13 13:52.657 ..." (子级)"...从c:\ temp \中读取AppINI.xml" (子级)"... GetSettings-已完成"

(parent) "Begin GetSettings 2012-03-13 13:52.657..."  (child) "...Read AppINI.xml from c:\temp\" (child) "...GetSettings - completed"

(父)开始GetNextRecord 2012-03-13 13:53.089 ..." (孩子)"... found youme@mycoinc.com"; (子级)"...正在构建ctctLists"

(parent) "Begin GetNextRecord 2012-03-13 13:53.089..." (child) "...Found youme@mycoinc.com" (child) "...Building ctctLists"

JeffP ...

JeffP...

推荐答案

嗨杰夫,

   

    

如果我是我,我会坚持使用TreeView/ObservableCollection解决方案.

I'd stick with the TreeView/ObservableCollection solution, if I were you.

下面是一个非常简单的可以正常工作的示例,它是我认为您要尝试执行的操作:

Below is a very simple fully working example of what I think you are trying to do:

  

  

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" FontSize="16"
        xmlns:local="clr-namespace:WpfApplication1">
    <StackPanel>
        <TreeView ItemsSource="{Binding ModuleReports}" >

            <TreeView.ItemContainerStyle>
                <Style>
                    <Setter Property="TreeViewItem.IsExpanded" Value="True"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=Type}" Value="menu">
                            <Setter Property="TreeViewItem.IsSelected" Value="True"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>

            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local:ModuleReporter}" ItemsSource="{Binding Children}">
                    <StackPanel>
                        <TextBlock Text="{Binding Path=ModuleName}" />
                        <ListBox ItemsSource="{Binding ModuleMessages}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>
    </StackPanel>
</Window>

   

    

using System;
using System.Windows;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    public class ModuleReporter
    {
        public ModuleReporter()
        {
            ModuleMessages = new ObservableCollection<string>();
        }

        public string ModuleName { get; set; }
        public ObservableCollection<string> ModuleMessages { get; set; }
        public ObservableCollection<ModuleReporter> Children { get; set; }
    }

    public partial class MainWindow : Window
    {
        public ObservableCollection<ModuleReporter> ModuleReports { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            ModuleReports = new ObservableCollection<ModuleReporter>
            {
                new ModuleReporter
                {
                    ModuleName = "Level 1a",
                    Children = new ObservableCollection<ModuleReporter>
                    {
                        new ModuleReporter
                        {
                            ModuleName = "Level 2a",
                            Children = null
                        },
                        new ModuleReporter
                        {
                            ModuleName = "Level 2b",
                            Children = null
                        },
                    }
                }
            };
            
            this.DataContext = this;

            StartModule();
        }

        Random rand = new Random(DateTime.Now.Second);
        int cnt;

        private void StartModule()
        {
            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(500);
                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => 
                {
                    ModuleReports[0].Children[rand.Next(0, 2)].ModuleMessages.Add(DateTime.Now.ToString());
                }));
            }).ContinueWith(ret => 
            {
                if (cnt++ < 10)
                    StartModule(); 
            });
        }

    }
}

   

   

   

    

因此,在我的示例中,我正在创建TreeView结构所需的分层对象.然后,我为每个项目定义一个基本模板.上面还有一些ItemContainer代码,只是使TreeView项在加载时扩展,因此您可以看到更新 当它们进入时,我正在使用暂停和更新"该示例只是随机地更新了两个孩子,但是更新代码也可能使新数据"冒泡.直到父类的事件,而这些事件又可能会更新.

So in my example, I am creating the heiarchical object required for the TreeView structure. I am then defining a basic template for each item. Also above is some ItemContainer code, simply to make the TreeView items expanded on load, so you can see the updates as they come in. I am using a "pause and update" from a background thread to demonstrate data coming in. The example just randomly updates two children, but the update code could also bubble "new data" events up to the parent class, which could in turn update.

   

    

我希望这会有所帮助.

  

   

此致,
皮特

Regards,
Pete


这篇关于使用Treeview和Observable集合的过程监视器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆