Another TreeView HierarchicalDataTemplate

摘要:Another TreeView HierarchicalDataTemplate

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="DJRepMonitorWPF4.Window1"
        Title="DJRepMonitorWPF4" Height="510" Width="819"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        x:Name="RepMonitor"
        Loaded="OnRepMonitorLoad"
        KeyUp="OnRepMonitorKeyUp"
        KeyDown="OnRepMonitorKeyDown"
        xmlns:DJRepMonitor="clr-namespace:DJRepMonitorWPF4">
    <Window.Resources>
        <Storyboard x:Key="OnMainLoad">
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ReporterTree" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
                <SplineDoubleKeyFrame KeyTime="00:00:01" Value="360"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ReportList" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)">
                <SplineDoubleKeyFrame KeyTime="00:00:01.5000000" Value="360"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
       
        <!--
        <XmlDataProvider x:Key="ReportServerDS" d:IsDataSource="True" Source="E:\Src\MoneyDJ\SysJust\DJSysMon\DJRepMonitorWPF4\ReportServer.xml"/>
        -->
        <!--       
        <HierarchicalDataTemplate x:Key="NodeTemplate">
            <HierarchicalDataTemplate.ItemsSource>
                <Binding Path="DataItems"/>
            </HierarchicalDataTemplate.ItemsSource>
            <Border Margin="2" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="4">
                <StackPanel Orientation="Horizontal">
                    <Ellipse Margin="1" Width="12" Height="12"  Fill="Red" />
                    <TextBlock Margin="1,1,1,1" FontWeight="DemiBold"  VerticalAlignment="Center" Text="{Binding Path=Name}">
                    </TextBlock>
                </StackPanel>
            </Border>
        </HierarchicalDataTemplate>
        -->
        <HierarchicalDataTemplate DataType="{x:Type DJRepMonitor:ReporterCfg}">
            <HierarchicalDataTemplate.ItemsSource>
                <Binding Path="DataItems"/>               
            </HierarchicalDataTemplate.ItemsSource>
            <Border Margin="1" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="4">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Margin="2,2,2,2" FontWeight="DemiBold" VerticalAlignment="Center" Text="{Binding Path=Name}" />
                    <Button Content=" ... " Margin="4,0,0,0" Click="ReporterCfg_Clicked" Tag="{Binding}"/>
                </StackPanel>
            </Border>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type DJRepMonitor:GroupCfg}">
            <HierarchicalDataTemplate.ItemsSource>
                <Binding Path="DataItems"/>
            </HierarchicalDataTemplate.ItemsSource>
            <Border Margin="1" BorderThickness="1" BorderBrush="SteelBlue"  CornerRadius="4">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Margin="2,2,2,2" FontWeight="DemiBold"  VerticalAlignment="Center" Text="{Binding Path=Name}" />
                    <Button Content=" ... " Margin="4,0,0,0" Click="GroupCfg_Clicked" Tag="{Binding}"/>
                </StackPanel>
            </Border>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type DJRepMonitor:Server}">
            <HierarchicalDataTemplate.ItemsSource>
                <Binding Path="DataItems"/>
            </HierarchicalDataTemplate.ItemsSource>
            <Border Margin="2" BorderThickness="1" BorderBrush="IndianRed"  CornerRadius="4">
                <StackPanel Orientation="Horizontal">
                    <Ellipse Margin="1" Width="12" Height="12"  Fill="Red" />
                    <TextBlock Margin="2,2,2,2" FontWeight="DemiBold"  VerticalAlignment="Center" Text="{Binding Path=Name}" />
                </StackPanel>
            </Border>
        </HierarchicalDataTemplate>
        <Storyboard x:Key="OnSelectedItemChanged1">
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="ReporterTree" Storyboard.TargetProperty="(TextElement.FontSize)">
                <SplineDoubleKeyFrame KeyTime="00:00:00.2000000" Value="13"/>
                <SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="12"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Style x:Key="TreeViewStyle1" TargetType="{x:Type TreeView}">
            <Style.Triggers>
            </Style.Triggers>
        </Style>
        <!--       
        <ObjectDataProvider x:Key="ODPRepoter" ObjectType="{x:Type DJRepMonitor:ReporterCfg}"/>
        <ObjectDataProvider x:Key="ODPGroup" ObjectType="{x:Type DJRepMonitor:GroupCfg}"/>
        <ObjectDataProvider x:Key="ODPServer" ObjectType="{x:Type DJRepMonitor:Server}"/>

        <DataTemplate x:Key="ReporterTVItem">
            <DockPanel>
                <Image Source="images\icon.jpg"/>
                <TextBlock VerticalAlignment="center" Text ="{Binding Path=Name}"/>
            </DockPanel>
        </DataTemplate>
-->
    </Window.Resources>
    <Window.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FF000000" Offset="0"/>
            <GradientStop Color="#FF80828B" Offset="0.138"/>
        </LinearGradientBrush>
    </Window.Background>
    <Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource OnMainLoad}" x:Name="OnMainLoad_BeginStoryboard"/>
        </EventTrigger>
        <EventTrigger RoutedEvent="TreeView.SelectedItemChanged" SourceName="ReporterTree">
            <BeginStoryboard Storyboard="{StaticResource OnSelectedItemChanged1}" x:Name="OnSelectedItemChanged1_BeginStoryboard"/>
        </EventTrigger>
    </Window.Triggers>
   
    <Grid x:Name="gridLayout">
        <Grid.RowDefinitions>
            <RowDefinition Height="47.124"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
       
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="210"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TreeView RenderTransformOrigin="0.5,0.5"
                  Margin="8,0,8,8"
                  x:Name="ReporterTree"
                  SelectedItemChanged="OnReporterTreeSelectedItemChanged"
                  Grid.Row="1"
                  HorizontalContentAlignment="Stretch" BorderThickness="2,2,2,2" Style="{DynamicResource TreeViewStyle1}"                 
                  >
                <!-- ItemsSource="{x:Static DJRepMonitor:Window1.config}" -->
                <!-- ItemTemplate="{StaticResource NodeTemplate}" -->
            <TreeView.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleX="1" ScaleY="1"/>
                    <SkewTransform AngleX="0" AngleY="0"/>
                    <RotateTransform Angle="0"/>
                    <TranslateTransform X="0" Y="0"/>
                </TransformGroup>
            </TreeView.RenderTransform>
            <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded"  Value="{Binding Expanded}"/>
                </Style>
            </TreeView.ItemContainerStyle>           
        </TreeView>
        <ListView RenderTransformOrigin="0.5,0.5"
                  IsSynchronizedWithCurrentItem="True"
                  Margin="0,0,8,8"
                  x:Name="ReportList"
                  Grid.Column="1"
                  Grid.Row="1" BorderThickness="2,2,2,2">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="GridViewColumn"/>
                </GridView>
            </ListView.View>
            <ListView.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleX="1" ScaleY="1"/>
                    <SkewTransform AngleX="0" AngleY="0"/>
                    <RotateTransform Angle="0"/>
                    <TranslateTransform X="0" Y="0"/>
                </TransformGroup>
            </ListView.RenderTransform>
        </ListView>
        <Button HorizontalAlignment="Right" Margin="0,0,8,0" Width="53" Content="Button" Grid.Column="1" x:Name="btnLogo"/>
        <CheckBox HorizontalAlignment="Right" Margin="0,15.582,65,14.418" Width="78" Content="CheckBox" Grid.Column="1" x:Name="chkTopmost" Foreground="#FFFFFFFF"/>
       
    </Grid>
</Window>
 

config = (ReporterConfig)System.Configuration.ConfigurationManager.GetSection("ReporterConfig");

ReporterTree.ItemsSource = config;
ReporterTree.SnapsToDevicePixels = true;

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Configuration;
using DJLibrary.Utility;
using System.Collections;
using System.Collections.ObjectModel;

namespace DJRepMonitorWPF4
{
    public class ReporterConfigSectionHandler : IConfigurationSectionHandler
    {
        public ReporterConfigSectionHandler()
        {
        }

        public object Create(object parent, object configContext, XmlNode section)
        {
            ReporterConfig config = new ReporterConfig();
            if (section != null)
                config.Load(section);

            return config;
        }
    }

    public class ReporterConfig : ObservableCollection<ReporterCfg>
    {
        public List<IDDef> m_arIDDef = new List<IDDef>();

        public IList<ReporterCfg> DataItems
        {
            get { return Items; }
        }

        public void Load(XmlNode node)
        {
            XmlNodeList nodeList = node.SelectNodes("Repoter");
            foreach (XmlNode nodeReporter in nodeList)
            {
                ReporterCfg cfgReporter = new ReporterCfg();

                cfgReporter.Name = XmlUtil.GetNodeValue_String(nodeReporter, "Name");
                cfgReporter.Url = XmlUtil.GetNodeValue_String(nodeReporter, "ReportUrl");

                // Group
                XmlNodeList nodeListGroup = nodeReporter.SelectNodes("Groups/Group");
                foreach (XmlNode nodeGroup in nodeListGroup)
                {
                    GroupCfg cfgGroup = new GroupCfg();

                    cfgGroup.Name = XmlUtil.GetNodeValue_String(nodeGroup, "@Name");

                    XmlNodeList nodeListServer = nodeGroup.SelectNodes("Server");
                    foreach (XmlNode nodeServer in nodeListServer)
                    {
                        string strIP = XmlUtil.GetNodeValue_String(nodeServer, "@IP");
                        string strNameServer = XmlUtil.GetNodeValue_String(nodeServer, "@Name");

                        cfgGroup.Add(new Server(strIP, strNameServer));
                    }
                    cfgReporter.Add(cfgGroup);
                }
                // Monitor
                XmlNode nodeMonitor = nodeReporter.SelectSingleNode("Monitor");
                string strIDList = XmlUtil.GetNodeValue_String(nodeMonitor, "@IDList");
                if (strIDList.Length > 0)
                {
                    cfgReporter.MonitorID = strIDList.Split(new char[] { ',' });
                    //string []arID = strIDList.Split(new char[] { ',' });
                }
                else
                {
                    cfgReporter.MonitorID = null;
                }

                XmlNodeList nodeListRule = nodeMonitor.SelectNodes("Rule");
                foreach (XmlNode nodeRule in nodeListRule)
                {
                    Rule rule = new Rule();

                    rule.m_strID = XmlUtil.GetNodeValue_String(nodeRule, "@ID");
                    string strValueMin = XmlUtil.GetNodeValue_String(nodeRule, "@Min");
                    if (strValueMin != null && strValueMin.Length > 0)
                        rule.m_dbMin = Double.Parse(strValueMin);

                    string strValueMax = XmlUtil.GetNodeValue_String(nodeRule, "@Max");
                    if (strValueMax != null && strValueMax.Length > 0)
                        rule.m_dbMax = Double.Parse(strValueMax);

                    rule.m_areaAlert = (AlertArea)Int32.Parse(XmlUtil.GetNodeValue_String(nodeRule, "@AlertArea"));

                    string strAlertType = XmlUtil.GetNodeValue_String(nodeRule, "@AlertType");
                    rule.m_arAlertID.Clear();
                    if (strAlertType.Length > 0)
                    {
                        rule.m_arAlertID.AddRange(strAlertType.Split(new char[] { ',' }));
                    }

                    cfgReporter.m_arRule.Add(rule);
                }

                // AlertDef
                XmlNodeList nodeListAlertDef = nodeReporter.SelectNodes("AlertDef/Alert");
                foreach (XmlNode nodeAlertDef in nodeListAlertDef)
                {
                    string strType = XmlUtil.GetNodeValue_String(nodeAlertDef, "@Type");
                    string strParam0 = XmlUtil.GetNodeValue_String(nodeAlertDef, "@Param0");
                    string strParam1 = XmlUtil.GetNodeValue_String(nodeAlertDef, "@Param1");

                    cfgReporter.m_dictAlert[strType] = new AlertDef(strType, strParam0, strParam1);
                }

                this.Add(cfgReporter);
            }

            XmlNodeList nodeListIDDef = node.SelectNodes("IDDefine/Item");
            foreach (XmlNode nodeIDDef in nodeListIDDef)
            {
                string strIDDef = XmlUtil.GetNodeValue_String(nodeIDDef, "@ID");
                string strIDDefName = XmlUtil.GetNodeValue_String(nodeIDDef, "@Name");
                m_arIDDef.Add(new IDDef(strIDDef, strIDDefName));
            }
        }
    }

    public enum AlertArea
    {
        UnderMin = 0,
        BetweenMinMax = 1,
        OverMax = 2,
        Undefine = 99
    }

    public class ReporterCfg : ObservableCollection<GroupCfg>
    {
        string m_strName = "";
        string m_strUrl = "";

        ArrayList m_arMonitorID = new ArrayList();

        public List<Rule> m_arRule = new List<Rule>();

        public Dictionary<string, AlertDef> m_dictAlert = new Dictionary<string, AlertDef>();

        public IList<GroupCfg> DataItems
        {
            get { return Items; }
        }

        public string Name
        {
            get { return m_strName; }
            set { m_strName = value; }
        }
        public string Url
        {
            get { return m_strUrl; }
            set { m_strUrl = value; }
        }
        public string[] MonitorID
        {
            get { return (string[])m_arMonitorID.ToArray(); }
            set
            {
                m_arMonitorID.Clear();
                if (value != null)
                    m_arMonitorID.AddRange(value);
            }
        }
    }

    public class GroupCfg : ObservableCollection<Server>
    {
        string m_strName = "";
        public string Name
        {
            get { return m_strName; }
            set { m_strName = value; }
        }
        public IList<Server> DataItems
        {
            get { return Items; }
        }
    }

    public class Server
    {
        string m_strIP = "";
        string m_strName = "";

        public Server(string strIP, string strName)
        {
            m_strIP = strIP;
            m_strName = strName;
        }
        public string IP
        {
            get { return m_strIP; }
            set { m_strIP = value; }
        }
        public string Name
        {
            get { return m_strName.Length == 0 ? m_strIP : m_strName; }
            set { m_strName = value; }
        }
    }

    public class Rule
    {
        public string m_strID = "";
        public double m_dbMin = Double.MinValue;
        public double m_dbMax = Double.MaxValue;
        public AlertArea m_areaAlert = AlertArea.Undefine;
        public ArrayList m_arAlertID = new ArrayList();
        public Rule()
        {
        }
    }

    public class AlertDef
    {
        string m_strType = "";
        string m_strParam0 = "";
        string m_strParam1 = "";

        public AlertDef(string strType, string strParam0, string strParam1)
        {
            m_strType = strType;
            m_strParam0 = strParam0;
            m_strParam1 = strParam1;
        }
    }

    public class IDDef
    {
        string m_strID = "", m_strName = "";

        public IDDef(string strID, string strName)
        {
            m_strID = strID;
            m_strName = strName;
        }
        public string ID
        {
            get { return m_strID; }
        }
        public string Name
        {
            get { return m_strName; }
        }
    }
}