Binding to Dictionary

8 posts, 0 answers
  1. ZooY
    ZooY avatar
    11 posts
    Member since:
    Nov 2011

    Posted 21 Jan 2013 Link to this post

    Can I Binding PivotGrid to List<Dictionary<>>?
    Or any other objects with unknown struture (dynamic class for example)?
  2. Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 23 Jan 2013 Link to this post

    Hi,

    You cannot use List<Dictionary>. If you want to create properties dynamically, you can use ICustomTypeProvider (to use it you'll have to download our next week's internal build as we have just fixed some issues with it).

    Thank you for choosing RadPivotGrid. Do not hesitate to contact us if you have any suggestions, concerns or problems.

    All the best,
    Rosen Vladimirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. ZooY
    ZooY avatar
    11 posts
    Member since:
    Nov 2011

    Posted 28 Jan 2013 Link to this post

    How to know when released this "next week's internal build"?
  5. Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 28 Jan 2013 Link to this post

    Hello,

    You can check under your account. The internal build should be available for download later today or tomorrow.

    Hopefully this helps.

    Kind regards,
    Rosen Vladimirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. ZooY
    ZooY avatar
    11 posts
    Member since:
    Nov 2011

    Posted 01 Feb 2013 Link to this post

    I downloaded, updated, checked ... does not work. Maybe I'm doing something wrong? ..

    public class MyDataRow : DynamicObject, INotifyPropertyChanged
    {
        readonly IDictionary<string, object> data;

        public MyDataRow()
        {
            data = new Dictionary<string, object>();
        }

        public MyDataRow(IDictionary<string, object> source)
        {
            data = source;
        }

        public override IEnumerable<string> GetDynamicMemberNames()
        {
            return data.Keys;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = this[binder.Name];

            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            this[binder.Name] = value;

            return true;
        }

        public object this[string columnName]
        {
            get
            {
                if (data.ContainsKey(columnName))
                {
                    return data[columnName];
                }

                return null;
            }
            set
            {
                if (!data.ContainsKey(columnName))
                {
                    data.Add(columnName, value);

                    OnPropertyChanged(columnName);
                }
                else
                {
                    if (data[columnName] != value)
                    {
                        data[columnName] = value;

                        OnPropertyChanged(columnName);
                    }
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }

    public class MyDataContext : ViewModelBase
    {
        private readonly IList<Dictionary<string, object>> _pivotData = new List<Dictionary<string, object>>
        {
            new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 1, 1, 0, 0, 0) }, { "Price", 10.40 }, { "Quantity", 148 } },
            new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 2, 1, 0, 0, 0) }, { "Price", 10.99}, { "Quantity", 122 }},
            new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 3, 1, 0, 0, 0) }, { "Price", 11.24}, { "Quantity", 80 }},
            new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 4, 1, 0, 0, 0) }, { "Price", 11.24}, { "Quantity", 90 }},
            new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 5, 1, 0, 0, 0) }, { "Price", 11.14}, { "Quantity", 140 }},
            new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 6, 1, 0, 0, 0) }, { "Price", 10.89}, { "Quantity", 162 }},
            new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 7, 1, 0, 0, 0) }, { "Price", 10.89}, { "Quantity", 181 }},
            new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 8, 1, 0, 0, 0) }, { "Price", 10.88}, { "Quantity", 180 }},
            new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 9, 1, 0, 0, 0) }, { "Price", 11.00}, { "Quantity", 116 }},
            new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 10, 1, 0, 0, 0) }, { "Price", 10.99}, { "Quantity", 128 }},
            new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 11, 1, 0, 0, 0) }, { "Price", 10.95}, { "Quantity", 145 }},
            new Dictionary<string, object> { { "Name", "Pen" }, { "Date", new DateTime(2012, 12, 1, 0, 0, 0) }, { "Price", 10.45}, { "Quantity", 189 }},
            new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 1, 1, 0, 0, 0) }, { "Price", 5.22}, { "Quantity", 100 }},
            new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 2, 1, 0, 0, 0) }, { "Price", 5.99}, { "Quantity", 85 }},
            new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 3, 1, 0, 0, 0) }, { "Price", 6.04}, { "Quantity", 80 }},
            new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 4, 1, 0, 0, 0) }, { "Price", 6.28}, { "Quantity", 72 }},
            new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 5, 1, 0, 0, 0) }, { "Price", 6.12}, { "Quantity", 99 }},
            new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 6, 1, 0, 0, 0) }, { "Price", 6.59}, { "Quantity", 40 }},
            new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 7, 1, 0, 0, 0) }, { "Price", 6.29}, { "Quantity", 68 }},
            new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 8, 1, 0, 0, 0) }, { "Price", 5.99}, { "Quantity", 95 }},
            new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 9, 1, 0, 0, 0) }, { "Price", 5.89}, { "Quantity", 120 }},
            new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 10, 1, 0, 0, 0) }, { "Price", 5.99}, { "Quantity", 105 }},
            new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 11, 1, 0, 0, 0) }, { "Price", 5.96}, { "Quantity", 111 }},
            new Dictionary<string, object> { { "Name", "Pencil" }, { "Date", new DateTime(2012, 12, 1, 0, 0, 0) }, { "Price", 5.99}, { "Quantity", 108 }},
            new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 1, 1, 0, 0, 0) }, { "Price", 22.86}, { "Quantity", 88 }},
            new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 2, 1, 0, 0, 0) }, { "Price", 23.02}, { "Quantity", 95 }},
            new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 3, 1, 0, 0, 0) }, { "Price", 23.22}, { "Quantity", 102 }},
            new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 4, 1, 0, 0, 0) }, { "Price", 21.99}, { "Quantity", 95 }},
            new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 5, 1, 0, 0, 0) }, { "Price", 22.45}, { "Quantity", 84 }},
            new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 6, 1, 0, 0, 0) }, { "Price", 22.56}, { "Quantity", 96 }},
            new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 7, 1, 0, 0, 0) }, { "Price", 22.88}, { "Quantity", 88 }},
            new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 8, 1, 0, 0, 0) }, { "Price", 22.42}, { "Quantity", 99 }},
            new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 9, 1, 0, 0, 0) }, { "Price", 22.56}, { "Quantity", 111 }},
            new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 10, 1, 0, 0, 0) }, { "Price", 22.18}, { "Quantity", 102 }},
            new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 11, 1, 0, 0, 0) }, { "Price", 22.93}, { "Quantity", 105 }},
            new Dictionary<string, object> { { "Name", "Notebook" }, { "Date", new DateTime(2012, 12, 1, 0, 0, 0) }, { "Price", 22.89}, { "Quantity", 122 }}
        };
         
            
        IEnumerable<MyDataRow> _data;
        public IEnumerable<MyDataRow> Data
        {
            get
            {
                if (_data == null)
                {
                    _data = new List<MyDataRow>(from i in _pivotData select new MyDataRow(i));

                    OnPropertyChanged("Data");
                }
                return _data;
            }
        }
    }

    <UserControl xmlns:pivot="http://schemas.telerik.com/2008/xaml/presentation/pivot"  x:Class="RadControlsSilverlightApp1.MainPage"
    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"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

        <UserControl.Resources>
            <pivot:LocalDataSourceProvider x:Key="LocalDataProvider">
                <pivot:LocalDataSourceProvider.RowGroupDescriptions>
                    <pivot:PropertyGroupDescription  PropertyName="Name"/>
                </pivot:LocalDataSourceProvider.RowGroupDescriptions>
                <pivot:LocalDataSourceProvider.ColumnGroupDescriptions>
                    <pivot:PropertyGroupDescription  PropertyName="Date"/>
                </pivot:LocalDataSourceProvider.ColumnGroupDescriptions>
                <pivot:LocalDataSourceProvider.AggregateDescriptions>
                    <pivot:PropertyAggregateDescription PropertyName="Quantity"/>
                </pivot:LocalDataSourceProvider.AggregateDescriptions>
            </pivot:LocalDataSourceProvider>
        </UserControl.Resources>

        <Grid x:Name="LayoutRoot">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <pivot:RadPivotGrid DataProvider="{StaticResource LocalDataProvider}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
            <pivot:RadPivotFieldList VerticalAlignment="Stretch" HorizontalAlignment="Stretch" DataProvider="{StaticResource LocalDataProvider}" Grid.Column="1" />        
    </Grid>
    </UserControl>

  7. Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 01 Feb 2013 Link to this post

    Hello,

    I'm sorry, but it looks like there is a misunderstanding. When I said, that you'll be able to use "it" from the next week, by "it" I meant ICustomTypeProvider. If you want to use dynamic properties, you should do it with ICustomTypeProvider. More information about this interface can be found here. As previously declared - with the current release you are not able to use Dictionary for ItemsSource.

    Once again sorry for the misunderstanding. Do not hesitate to contact us if you have any problems or concerns.

    Kind regards,
    Rosen Vladimirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  8. Pavel
    Pavel avatar
    2 posts
    Member since:
    Nov 2007

    Posted 04 Feb 2013 Link to this post

    I tried to use ICustomTypeProvider, use CustomTypeHelper by your link.
    Changed the code as follows:

    public class MyDataContext : ViewModelBase
    {
            ...
            ObservableCollection<MyDataRow> _data;
            public ObservableCollection<MyDataRow> Data
            {
                get
                {
                    if (_data == null)
                    {
                        _data = new ObservableCollection<MyDataRow>();
                        MyDataRow.AddProperty("Name", typeof(string));
                        MyDataRow.AddProperty("Date", typeof(DateTime));
                        MyDataRow.AddProperty("Price", typeof(decimal));
                        MyDataRow.AddProperty("Quantity", typeof(int));
                        foreach (var sourceItem in PivotData)
                        {
                            var dataItem = new MyDataRow();
                            foreach (var si in sourceItem)
                                dataItem.SetPropertyValue(si.Key, si.Value);
                            _data.Add(dataItem);
                        }
                        OnPropertyChanged("Data");
                    }
                    return _data;
                }
            }
            ...
    }

    public class MyDataRow : CustomTypeHelper<MyDataRow>
    {
    }

    Still does not work.
  9. Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 04 Feb 2013 Link to this post

    Hi Pavel,

    Did you download our latest internal build? Using ICustomTypeProvider is available only if you are using it. I'm sending you a project where if you click the button you will see two RadPivotGrids and both of them have some dynamic properties. You can use the implementation for your reference.

    Hopefully this helps.

    All the best,
    Rosen Vladimirov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
DevCraft banner