This is a migrated thread and some comments may be shown as answers.

Binding to GridView Selected Item which an array

3 Answers 311 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Richard Harrigan
Top achievements
Rank 1
Richard Harrigan asked on 29 Dec 2011, 10:28 PM
Hi

I have succesfully bound an array to a gridview (see screenshot).  However I am not able to bind the selected Grid item to the PropertyGrid.  Is is because of the array?  If I need to do some code-behind can you provide a code snippet.

Thanks
Rich

XAML follows:

<UserControl
             x:Class="RadGridView_SL4_AR_25.MainPage"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:my="clr-namespace:RadGridView_SL4_AR_25"
             xmlns:telerikQuickStart="clr-namespace:Telerik.Windows.Controls.QuickStart;assembly=Telerik.Windows.Controls"
             mc:Ignorable="d" d:DesignHeight="700" d:DesignWidth="700">
    <UserControl.Resources>
        <my:MyViewModel x:Key="MyViewModel"/>
    </UserControl.Resources>
     
    <Grid x:Name="LayoutRoot"
          Background="White"
          DataContext="{StaticResource MyViewModel}">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="350" />
        </Grid.ColumnDefinitions>
 
        <telerik:RadGridView
            Name="myGrid"
            ItemsSource="{Binding GridItems, Mode=TwoWay}"
            SelectionChanged="myGrid_SelectionChanged"
            CanUserSortColumns="True"
            IsReadOnly="True"
            ShowGroupPanel="True"
            Loaded="myGrid_Loaded" 
            AutoGenerateColumns="False">
            
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Columns[0]}"
                                            Header="Name"
                                            UniqueName="Customer_Name" />
 
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Columns[1]}"
                                            Header="Street"
                                            UniqueName="Customer_Street" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Columns[2]}"
                                            Header="City"
                                            UniqueName="Customer_City" />
 
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Columns[3]}"
                                            Header="State"
                                            UniqueName="Customer_State" />
 
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Columns[4]}"
                                            Header="Zip Code"
                                            UniqueName="Customer_ZipCode" />
 
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
 
        <telerik:RadPropertyGrid x:Name="RadPropertyGrid1"
                                 Item="{Binding SelectedItem, ElementName=myGrid, Mode=TwoWay}"
                                 Margin="8"
                                 LabelColumnWidth="130"
                                 Grid.Column="1"
                                 AutoGeneratePropertyDefinitions="False">
            <telerik:RadPropertyGrid.PropertyDefinitions>
                <telerik:PropertyDefinition DisplayName="Name"
                                            Binding="{Binding Columns[0]}" />
                <telerik:PropertyDefinition DisplayName="Street"
                                            Binding="{Binding Columns[1]}" />
                <telerik:PropertyDefinition DisplayName="City"
                                            Binding="{Binding Columns[2]}" />
                <telerik:PropertyDefinition DisplayName="State"
                                            Binding="{Binding Columns[3]}" />
                <telerik:PropertyDefinition DisplayName="Postal Code"
                                            Binding="{Binding Columns[4]}" />
            </telerik:RadPropertyGrid.PropertyDefinitions>
 
        </telerik:RadPropertyGrid>
 
    </Grid>
</UserControl>


3 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 30 Dec 2011, 12:54 PM
Hello Richard, 

I have tried to reproduce the issue you reported, but still without any success.Could you take a look at the sample attached and let me know whether you can get the same behavior on it ? What is the exact type of your source ? Am I missing something according to your  requirements ? 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Richard Harrigan
Top achievements
Rank 1
answered on 30 Dec 2011, 05:55 PM
Hi Maya,

Your example worked for me.  The primary difference between out examples is that in your example the data source is Club[] GridItems and my example is ObservableCollection<CRow> GridItems where CRow has only one property List<object> Columns and where each column is refered to by index rather than property name for binding as in your example.  

I also tried generating the DataColumns and PropertyDefinitions in code instead of the xaml and got the same result.  The advantage of creating the DataColumns in code was the ability to specify the datatype. 

If you might, please try to run my example. You have the xaml from my previous post and the code to create the data are as follows.

CRow
ColumnMetadata
MyViewModel

Thanks
Rich
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System;
namespace RadGridView_SL4_AR_25
{
    #region Class CRow
 
    public class CRow
    {
 
        #region Properties
 
        private List<object> _Columns = new List<object>();
 
        public List<object> Columns
        {
            get { return _Columns; }
            set { _Columns = value;}
        }
 
        #endregion Properties
 
        #region methods
 
        #region GetRows
 
        public static ObservableCollection<CRow> GetRows()
        {
            ObservableCollection<CRow> rows = new ObservableCollection<CRow>();
            CRow row;
            List<object> columns = new List<object>();
            columns.Add("Lawrence Taylor");
            columns.Add("7230 Surfbird Lane");
            columns.Add("San Diego");
            columns.Add("CA");
            columns.Add(92011);
 
            row = new CRow() { Columns = columns };
            rows.Add(row);
 
            columns = new List<object>();
            columns.Add("Phil Simms");
            columns.Add("1800 Quarteback Way");
            columns.Add("New York");
            columns.Add("NY");
            columns.Add(73418);
 
            row = new CRow() { Columns = columns };
            rows.Add(row);
 
            columns = new List<object>();
            columns.Add("Mark Bavaro");
            columns.Add("412 End Lane");
            columns.Add("Philadelphia");
            columns.Add("PA");
            columns.Add(45722);
 
            row = new CRow() { Columns = columns };
            rows.Add(row);
  
            return rows;
        }
 
        #endregion GetRows
 
        #endregion methods
    }
 
    #endregion Class CRow
 
    public class ColumnMetadata
    {
        #region properties
 
        #region UniqueName
 
        string _UniqueName;
        public string UniqueName
        {
            get { return _UniqueName; }
            set { _UniqueName = value; }
        }
 
        #endregion UniqueName
 
        #region Header
 
        string _Header;
        public string Header
        {
            get { return _Header; }
            set { _Header = value; }
        }
 
        #endregion Header
 
        #region DataType
 
        string _DataType;
        public string DataType
        {
            get { return _DataType; }
            set { _DataType = value; }
        }
 
        #endregion DataType
 
        #endregion properties
 
        #region methods
 
        #region GetColumnMetadata
 
        public static ObservableCollection<ColumnMetadata> GetColumnMetadata()
        {
            ObservableCollection<ColumnMetadata> cols = new ObservableCollection<ColumnMetadata>();
 
            ColumnMetadata columnMetadata = new ColumnMetadata();
            columnMetadata.UniqueName = "Customer_Name";
            columnMetadata.Header = "Name";
            columnMetadata.DataType = "System.String";
            cols.Add(columnMetadata);
 
            columnMetadata = new ColumnMetadata();
            columnMetadata.UniqueName = "Customer_Street";
            columnMetadata.Header = "Street";
            columnMetadata.DataType = "System.String";
            cols.Add(columnMetadata);
 
            columnMetadata = new ColumnMetadata();
            columnMetadata.UniqueName = "Customer_City";
            columnMetadata.Header = "City";
            columnMetadata.DataType = "System.String";
            cols.Add(columnMetadata);
 
            columnMetadata = new ColumnMetadata();
            columnMetadata.UniqueName = "Customer_State";
            columnMetadata.Header = "State";
            columnMetadata.DataType = "System.String";
            cols.Add(columnMetadata);
 
            columnMetadata = new ColumnMetadata();
            columnMetadata.UniqueName = "Customer_ZipCode";
            columnMetadata.Header = "Zip Code";
            columnMetadata.DataType = "System.Int32";
            cols.Add(columnMetadata);
 
            return cols;
        }
 
        #endregion GetColumnMetadata
 
        #endregion methods
    }
}

using System.ComponentModel;
using System.Collections.ObjectModel;
 
namespace RadGridView_SL4_AR_25
{
    public class MyViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
 
        #region properties
 
        #region GridItems
 
        private ObservableCollection<CRow> _GridItems;
 
        public ObservableCollection<CRow> GridItems
        {
            get
            {
                return CRow.GetRows();
            }
 
            set
            {
                _GridItems = value;
                OnPropertyChanged("GridItems");
 
            }
        }
 
        #endregion GridItems
 
        #region ColumnItems
 
        private ObservableCollection<ColumnMetadata> _ColumnItems;
 
        public ObservableCollection<ColumnMetadata> ColumnItems
        {
            get
            {
                return ColumnMetadata.GetColumnMetadata();
            }
 
            set
            {
                _ColumnItems = value;
                OnPropertyChanged("ColumnItems");
            }
        }
 
        #endregion ColumnItems
 
        #endregion properties
 
        #region OnPropertyChanged
 
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, args);
            }
        }
 
        #endregion OnPropertyChanged
 
        private void OnPropertyChanged(string propertyName)
        {
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }
}



0
Maya
Telerik team
answered on 04 Jan 2012, 11:19 AM
Hi Richard,

Actually, this would be the expected behavior in this case since you are trying to bind the Item of RadPropertyGrid to a list. Thus the property grid neither can resolve what is its item, nor any properties that should be displayed. I would recommend you to follow the idea illustrated in the project I have previously attached - expose required properties in a business object. Thus the property grid will be able to resolve and display them properly.
 

Greetings,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
PropertyGrid
Asked by
Richard Harrigan
Top achievements
Rank 1
Answers by
Maya
Telerik team
Richard Harrigan
Top achievements
Rank 1
Share this question
or