locked
Code help RRS feed

  • Question

  • Cannot build. Who can help me? 

     

     

    C# Code:

    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 System.ComponentModel;
    using System.Windows.Data;

    namespace DynamicLoadingList
    {
        public partial class Page : UserControl
        {
            public Page()
            {
                InitializeComponent();

                List<Object> data = this.CreateData();

                this.myGrid.ItemsSource = data;

                DataGridTextColumn col;
                Binding b;
               
                col = new DataGridTextColumn();
                col.Header = "Resource";
                b = new Binding("Name");
                b.Mode = BindingMode.OneWay;
                col.Binding = b;

                this.myGrid.Columns.Add(col);

                int i=0;

                foreach (FiscalPeriod fp in data[GetListEntryWithMaxCount(data)].FiscalPeriods)
                {
                    col = new DataGridTextColumn();
                    col.Header = "Fiscal result " + i.ToString();
                    b = new Binding("FiscalPeriods");
                    b.Mode = BindingMode.OneWay;
                    b.Converter = new ListConverter();
                    b.ConverterParameter = i;
                    col.Binding = b;

                    this.myGrid.Columns.Add(col);

                    i++;
                }

            }

            private int GetListEntryWithMaxCount(List<Object> list)
            {
                int return_value = -1;

                int max_value = -1;
                int row_counter = -1;

                if (list.Count > 0)
                {
                    foreach (Resource item in list)
                    {
                        row_counter++;
                       
                        if (item.FiscalPeriods.Count > max_value)
                        {
                            max_value = item.FiscalPeriods.Count;
                            return_value = row_counter;
                        }
                    }
                }

                return return_value;
            }

            private List<Object> CreateData()
            {
                List<Object> data = new List<Object>();

                Resource item;
                FiscalPeriod period;

                item = new Resource();
                item.Id = "1";
                item.Name = "Resource 1";
                period = new FiscalPeriod();
                period.Hours = 1.0M;
                item.FiscalPeriods.Add(period);
                period = new FiscalPeriod();
                period.Hours = 2.0M;
                item.FiscalPeriods.Add(period);

                data.Add(item);

                item = new Resource();
                item.Id = "2";
                item.Name = "Resource 2";
                period = new FiscalPeriod();
                period.Hours = 2.0M;
                item.FiscalPeriods.Add(period);
                period = new FiscalPeriod();
                period.Hours = 3.0M;
                item.FiscalPeriods.Add(period);
                period = new FiscalPeriod();
                period.Hours = 4.0M;
                item.FiscalPeriods.Add(period);
                period = new FiscalPeriod();
                period.Hours = 5.0M;
                item.FiscalPeriods.Add(period);

                data.Add(item);

                item = new Resource();
                item.Id = "3";
                item.Name = "Resource 3";
                period = new FiscalPeriod();
                period.Hours = 3.0M;
                item.FiscalPeriods.Add(period);
                period = new FiscalPeriod();
                period.Hours = 4.0M;
                item.FiscalPeriods.Add(period);
                period = new FiscalPeriod();
                period.Hours = 5.0M;
                item.FiscalPeriods.Add(period);

                data.Add(item);

                return data;
            }
        }

        public class ListConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                List<Object> source = value as List<Object>;

                if (source != null && source.Count > (int)parameter)
                {
                    return source[(int)parameter].Hours;
                }
                else
                {
                    return null;
                }
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

        public class Resource
        {
            public string Id { get; set; }
            public string Name { get; set; }

            public List<Object> FiscalPeriods { get; set; }

            public Resource()
            {
                this.FiscalPeriods = new List<Object>();
            }
        }

        public class FiscalPeriod
        {
            public decimal Hours { get; set; }
        }
     
       
    }

     

     

     

     

    XAML:

     

    <UserControl x:Class="DynamicLoadingList.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
        Width="400" Height="300">
        <Grid x:Name="LayoutRoot" Background="White">
            <data:DataGrid x:Name="myGrid"></data:DataGrid>
        </Grid>
    </UserControl>

    Monday, April 20, 2009 6:01 PM

Answers