Fork me on GitHub

Telerik RadGanttView示例

TicksToTimeSpanConverter.cs
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Globalization;

namespace SGSGHome.Views.TaskTrace
{
    public class TicksToTimeSpanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return TimeSpan.FromTicks((long)(double)value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
<UserControl x:Class="SGSGHome.Views.TaskTrace.TaskGantt"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    xmlns:local="clr-namespace:SGSGHome.Views.TaskTrace"
    d:DesignWidth="1200" d:DesignHeight="600">
    <UserControl.Resources>
        <local:TicksToTimeSpanConverter x:Key="TicksToTimeSpanConverter" />
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Margin="3" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center">
            <TextBlock VerticalAlignment="Center">时间范围:从</TextBlock>
            <sdk:DatePicker Name="FromDate"></sdk:DatePicker>
            <TextBlock VerticalAlignment="Center">至</TextBlock>
            <sdk:DatePicker Name="ToDate"></sdk:DatePicker>
            <TextBlock VerticalAlignment="Center" Margin="3">项目名称:</TextBlock>
            <ComboBox Name="cbProject" Width="150" Height="20"></ComboBox>
            <Button Name="btnOK" Content="确定" Width="50" Height="22" Click="btnOK_Click" Margin="3,0,0,0"></Button>

            <!-- ZOOM slider-->
            <TextBlock VerticalAlignment="Center" Margin="3,0,0,0">放大</TextBlock>
            <Border BorderBrush="Gray" BorderThickness="1" Margin="2">
            <telerik:RadSlider  VerticalAlignment="Center" Width="120" x:Name="PixelLength"
                    Minimum="10000000" Maximum="60000000000" Value="35000000000"/>
            </Border>
            <TextBlock VerticalAlignment="Center">缩小</TextBlock>
        </StackPanel>
        <telerik:RadGanttView x:Name="ganttView" 
            HorizontalAlignment="Stretch" 
            VerticalAlignment="Stretch" 
            MouseLeftButtonDown="ganttView_MouseLeftButtonDown" 
            Grid.Row="1"
            PixelLength="{Binding Value, ElementName=PixelLength, Converter={StaticResource TicksToTimeSpanConverter}}">
            <telerik:RadGanttView.Columns>
                <telerik:TreeColumnDefinition Header="任务名称" />
                <telerik:ColumnDefinition MemberBinding="{Binding AssignTo}" Header="负责人" ColumnWidth="200" />
                <telerik:ColumnDefinition MemberBinding="{Binding ProgressString}" Header="任务进度" ColumnWidth="80" />
                <telerik:ColumnDefinition MemberBinding="{Binding HasDoc}" Header="文档"/>
                <telerik:ColumnDefinition MemberBinding="{Binding Start,StringFormat=yyyy-MM-dd}" Header="开始时间" ColumnWidth="100" />
                <telerik:ColumnDefinition MemberBinding="{Binding End,StringFormat=yyyy-MM-dd}" Header="预计完成时间" ColumnWidth="100" />
                <telerik:ColumnDefinition MemberBinding="{Binding ActualFinishDate,StringFormat=yyyy-MM-dd}" Header="实际完成时间" ColumnWidth="100" />
            </telerik:RadGanttView.Columns>
        </telerik:RadGanttView>
    </Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Telerik.Windows.Controls.GanttView;
using Telerik.Windows.Controls.Scheduling;
using System.Collections.ObjectModel;

namespace SGSGHome.Views.TaskTrace
{
    public class GanttTaskEx : GanttTask
    {
        public string AssignTo { get; set; }
        public string ProgressString { get; set; }
        public string PreTaskName { get; set; }
        public DateTime? ActualFinishDate { get; set; }
        public string HasDoc { get; set; }
        public GanttTaskEx() { }
        public GanttTaskEx(DateTime start, DateTime end, string title) : base(start, end, title) { }
    }

    public partial class TaskGantt : UserControl
    {
        public bool IsManager { get; set; }

        public ObservableCollection<TaskTraceService.TaskTrace> TaskTraceList { get; set; }
        public ObservableCollection<TaskTraceService.v_TaskDocCount> TaskDocCountList { get; set; }
        public GanttTaskEx Root { get; set; }
        TaskTraceService.TaskServiceClient client;
        CoreService.CoreServiceClient xClient;

        public TaskGantt()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(TaskGantt_Loaded);
        }

        void TaskGantt_Loaded(object sender, RoutedEventArgs e)
        {
            FromDate.SelectedDate = DateTime.Now.AddDays(-15);
            ToDate.SelectedDate = DateTime.Now.AddDays(15);
            this.TaskTraceList = new ObservableCollection<TaskTraceService.TaskTrace>();

            xClient = new CoreService.CoreServiceClient();
            xClient.HasRightCompleted += new EventHandler<CoreService.HasRightCompletedEventArgs>(xClient_HasRightCompleted);

            client = new TaskTraceService.TaskServiceClient();
            client.GetTaskByProjectNoCompleted += new EventHandler<TaskTraceService.GetTaskByProjectNoCompletedEventArgs>(client_GetTaskByProjectNoCompleted);
            client.GetTaskByMemberCompleted += new EventHandler<TaskTraceService.GetTaskByMemberCompletedEventArgs>(client_GetTaskByMemberCompleted);
            client.GetProjectNoListCompleted += new EventHandler<TaskTraceService.GetProjectNoListCompletedEventArgs>(client_GetProjectNoListCompleted);
            client.GetProjectNoListAsync();

            client.GetTaskDocCountCompleted += new EventHandler<TaskTraceService.GetTaskDocCountCompletedEventArgs>(client_GetTaskDocCountCompleted);
            client.GetTaskDocCountAsync();

        }

        void client_GetTaskDocCountCompleted(object sender, TaskTraceService.GetTaskDocCountCompletedEventArgs e)
        {
            TaskDocCountList = e.Result;
        }

        void client_GetTaskByProjectNoCompleted(object sender, TaskTraceService.GetTaskByProjectNoCompletedEventArgs e)
        {
            TaskTraceList = e.Result;
            if (e.Result.Count > 0)
            {
                FromDate.SelectedDate = e.Result.Min(x => x.StartDate).AddDays(-1);
                ToDate.SelectedDate = e.Result.Max(x => x.FinishDate).AddDays(1);
                this.Root = new GanttTaskEx(e.Result.Min(x => x.StartDate), e.Result.Max(x => x.FinishDate), ((TaskTraceService.ProjectBuild)cbProject.SelectedItem).ProjectName);
                AddChild(TaskTraceList, null, Root);
            }
            else
            {
                Root = null;
            }
        }

        void xClient_HasRightCompleted(object sender, CoreService.HasRightCompletedEventArgs e)
        {
            IsManager = AppContext.IsSupervisor || e.Result;
            cbProject_SelectionChanged(null, null);
        }

        void client_GetProjectNoListCompleted(object sender, TaskTraceService.GetProjectNoListCompletedEventArgs e)
        {
            if (e.Result.Count > 0)
            {
                cbProject.ItemsSource = e.Result;
                cbProject.DisplayMemberPath = "ProjectName";
                cbProject.SelectedValuePath = "ProjectNo";
                cbProject.SelectedIndex = 0;
                cbProject.SelectionChanged += new SelectionChangedEventHandler(cbProject_SelectionChanged);

                xClient.HasRightAsync(AppContext.LogonUserID, "SGSGHome.Views.TaskTrace.TaskManage", Operation.GetTagName(Operation.Tag.All));
            }
        }

        void cbProject_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (IsManager)
            {
                client.GetTaskByProjectNoAsync(cbProject.SelectedValue.ToString());
            }
            else
            {
                client.GetTaskByMemberAsync(AppContext.LogonUserName, cbProject.SelectedValue.ToString());
            }
        }

        void client_GetTaskByMemberCompleted(object sender, TaskTraceService.GetTaskByMemberCompletedEventArgs e)
        {
            TaskTraceList = e.Result;
            if (e.Result.Count > 0)
            {
                FromDate.SelectedDate = e.Result.Min(x => x.StartDate).AddDays(-1);
                ToDate.SelectedDate = e.Result.Max(x => x.FinishDate).AddDays(1);
                this.Root = new GanttTaskEx(e.Result.Min(x => x.StartDate), e.Result.Max(x => x.FinishDate), ((TaskTraceService.ProjectBuild)cbProject.SelectedItem).ProjectName);
                AddChild(TaskTraceList, null, Root);
            }
            else
            {
                Root = null;
            }
        }

        private void ganttView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                var r = ganttView.SelectedItem as GanttTaskEx;
                if (r != null && r.UniqueId != null)
                {
                    var w = new OutputDoc();
                    w.TaskTrace = TaskTraceList.Single(x => x.TaskID == long.Parse(r.UniqueId));
                    w.CurrentUser = AppContext.LogonUserName;
                    w.Show();
                }
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (FromDate.SelectedDate != null && ToDate.SelectedDate != null)
            {
                if (FromDate.SelectedDate.Value < ToDate.SelectedDate.Value)
                {
                    if (Root != null)
                    {
                        ganttView.VisibleRange = new VisibleRange(FromDate.SelectedDate.Value, ToDate.SelectedDate.Value);
                        ganttView.TasksSource = new ObservableCollection<GanttTaskEx>() { Root };
                    }
                    else
                    {
                        MessageBox.Show("没有数据!", "提示", MessageBoxButton.OK);
                    }
                }
            }
        }

        void AddChild(IList<TaskTraceService.TaskTrace> dataList, long? preTaskID, GanttTaskEx preTask)
        {
            var filterList = dataList.Where(x => x.PreTaskID == preTaskID).ToList();

            foreach (var tt in filterList)
            {
                var childTask = new GanttTaskEx()
                {
                    Start = tt.StartDate,
                    End = tt.FinishDate,
                    Title = tt.TaskName,
                    ActualFinishDate = tt.ActualFinishDate,
                    AssignTo = tt.AssignTo,
                    Progress = (double)tt.CompletePercent,
                    ProgressString = ((double)tt.CompletePercent).ToString() + "%",
                    UniqueId = tt.TaskID.ToString(),
                    PreTaskName = tt.PreTaskName,
                    HasDoc = TaskDocCountList.Any(x => x.TaskID == tt.TaskID) ? "" : ""
                };

                preTask.Children.Add(childTask);
                var childrenList = TaskTraceList.Where(x => x.PreTaskID == tt.TaskID).ToList();
                if (childrenList.Count > 0)
                {
                    AddChild(childrenList, tt.TaskID, childTask);
                }
            }
        }
    }
}

 

posted @ 2012-11-23 15:56  Nick.Chung  阅读(756)  评论(0编辑  收藏  举报