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

Displaying member in comboboxcolumn only after clicking column

3 Answers 49 Views
GridView
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 04 Sep 2011, 12:13 PM
I have a control, when I need dislay person with two column:
-fullname
-best friend


The problem is , that property BestFriend on Person is an object.
At start Person has his own BestFriend, but he can change it from combobox column. 


Now, after control loaded the column with bestfriend is blank.
When I doubleclick at this column I can change bestfirend, and it sets bestfriend of this person.


But what I must to do to have at start not blank column?


I think, that the problem is, that control can't match bestfriend, with collection of bestfriend, so I think that I must match them by id, but I don't know how can I do ti.






  
<UserControl x:Class="MvvmLight1.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"
               Height="300"
               Width="300"
               DataContext="{Binding Main, Source={StaticResource Locator}}">
      
   
   
              <UserControl.Resources>
          <ResourceDictionary>
              <ResourceDictionary.MergedDictionaries>
                  <ResourceDictionary Source="Skins/MainSkin.xaml" />
              </ResourceDictionary.MergedDictionaries>
          </ResourceDictionary>
      </UserControl.Resources>
   
      <Grid x:Name="LayoutRoot">
   
          <telerik:RadGridView x:Name="grdSrL"
                                                           AutoGenerateColumns="False"
                                                           SelectionMode="Single"
                                                           IsReadOnly="False"
                                                           IsFilteringAllowed="True"
                                                           Height="386"
                                                           Width="460"
                                                           HorizontalAlignment="Left"
                                                           CanUserDeleteRows="False"
                                                           CanUserInsertRows="True"
                                                           CanUserReorderColumns="False"
                                                           CanUserResizeColumns="True" ItemsSource="{Binding Persons}">
              <telerik:RadGridView.Columns>
                  <telerik:GridViewDataColumn DataMemberBinding="{Binding FullName}" IsReadOnly="True" Header="FullName" />
                  <telerik:GridViewComboBoxColumn ItemsSource="{Binding Friends,Source={StaticResource Main}}" ItemsSourceBinding="{Binding Friends,Source={StaticResource Main}}" Header="1st"
                                                  DataMemberBinding="{Binding BestFriend}"       
                           
                      DisplayMemberPath="FullName" />
   
   
              </telerik:RadGridView.Columns>
          </telerik:RadGridView>
      </Grid>
  </UserControl>


the main model:


  
namespace MvvmLight1
  {
      public class Person:INotifyPropertyChanged
      {
          private string _fullName;
   
          public string FullName
          {
              get { return _fullName; }
              set
              {
                  if (_fullName!=value)
                  {
                      _fullName = value;
                      OnPropertyChanged("FullName");
                  }
              }
          }
   
          public int Id
          {
              get { return _id; }
              set { _id = value; }
          }
   
          public Person BestFirend
          {
              get { return _bestFirend; }
              set
              {
                  if (_bestFirend!=value)
                  {
                      _bestFirend = value;
                      OnPropertyChanged("BestFirend");
                  }
              }
          }
   
          private int _id;
   
          private Person _bestFirend;
   
          public event PropertyChangedEventHandler PropertyChanged;
   
          protected virtual void OnPropertyChanged(string propertyName)
          {
              PropertyChangedEventHandler handler = PropertyChanged;
   
              if (handler != null)
              {
                  handler(this, new PropertyChangedEventArgs(propertyName));
              }
          }
      }
  }


and viewmodel:


using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;


using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;
 
namespace MvvmLight1.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        
        public MainViewModel()
        {
            for (int i = 0; i < 3; i++)
            {
                var friend = new Person() {FullName = "Name" + (i + 3).ToString()};
                _friends.Add(friend);
                _persons.Add(new Person(){FullName = "Name"+i.ToString(),Id = i,BestFirend = friend});
            }
        }
 
        private ObservableCollection<Person> _persons=new ObservableCollection<Person>();
 
 
        public ObservableCollection<Person> Persons
        {
            get { return _persons; }
            set
            {
                _persons = value;
            }
        }
 
        public ObservableCollection<Person> Friends
        {
            get { return _friends; }
            set
            {
                _friends = value;
            }
        }
 
        private ObservableCollection<Person> _friends=new ObservableCollection<Person>();
        
    }
}

and app xaml


             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="MvvmLight1.App"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:vm="clr-namespace:MvvmLight1.ViewModel"
             mc:Ignorable="d">
    <Application.Resources>
        <!--Global View Model Locator-->
        <vm:ViewModelLocator x:Key="Locator"
                             d:IsDataSource="True" />
       
        <vm:MainViewModel x:Key="Main"/>
    </Application.Resources>
</Application>

I'm using 2010.1.603.1040

3 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 05 Sep 2011, 08:33 AM
Hi John,

Firstly, there is no need to set ItemsSource and ItemsSourceBinding together for one GridViewComboBoxColumn. Secondly, you need to set the SelectedValueMemberPath property and the DataMemberBinding should point to a property, not a whole business object. 
Generally, I would recommend you to run through our online documentation and demos to check how you have to work with GridViewComboBoxColumn.
 

Regards,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
John
Top achievements
Rank 1
answered on 06 Sep 2011, 09:03 AM
Hi Maya,

Thanks for your reply.

Could you confirm, that I can't bind entire object to column?

Best regards,

John
0
Maya
Telerik team
answered on 07 Sep 2011, 07:08 AM
Hi John,

You need to bind a column to a single property, not to an entire object. In case you have some nested properties, you might set the DataMemberBinding to MyObject.Property1. Still, I would recommend you to bind a column directly to a property from your business object.
 

All the best,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
Maya
Telerik team
John
Top achievements
Rank 1
Share this question
or