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

I do not understand the GridViewComboBoxColumn properties

5 Answers 304 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andreas Bosch
Top achievements
Rank 1
Andreas Bosch asked on 18 Mar 2010, 06:10 PM
Hi,

after trying around for quite a while, I decided to post a message here because I just don't get the meaning of the different properties of a GridViewComboBoxColumn.

I have a MainViewModel with an ICollectionView property called "ItemsView" which is used as ItemsSource for my RadGridView. The ICollectionView contains items of type PersonViewModel. PersonViewModel has several properties, one of them is called "AgeGroup" and has the type EnumViewModel<EAgeGroup>. The EnumViewModel<TEnum> class has an object property called "LocalizedValue". The MainViewModel also has a property "AvailableAgeGroups" of type ObservableCollection<EnumViewModel<EAgeGroup>> which contains EnumViewModels for all available enum values of type EAgeGroup.

Here is some sample code showing all these classes and properties:

public class MainViewModel 
    public ICollectionView ItemsView { /* ... */ } 
 
    public ObservableCollection<EnumViewModel<EAgeGroup>> AvailableAgeGroups { /* ... */ } 
 
public class PersonViewModel  
{  
    public EnumViewModel<EAgeGroup> AgeGroup  
    {  
        get { /* ... */ }  
        set { /* ... */ }  
    }  
}  
  
public class EnumViewModel<TEnum>  
{  
    public object LocalizedValue { /* ... */ }  

As you can imagine, the EnumViewModel class serves as a "string/localization provider" for enums.

Now, I want to create a ComboBoxColumn which uses the "AvailableAgeGroups" collection as the ItemsSource for the ComboBoxes. The relevant property in the PersonViewModel is, of course, "AgeGroup". So, this property should decide which of the age groups is selected. But, the displayed value should be taken from the "LocalizedValue" property of the value from the "AgeGroup" property. To sum it up, I want to display "PersonViewModel.AgeGroup.LocalizedValue", but the actual item should be "PersonViewModel.AgeGroup".

Now, from what I know from typical .NET controls, I created this column as follows:
<telerik:GridViewComboBoxColumn 
    UniqueName="AgeGroup" 
    ItemsSource="{Binding AvailableAgeGroups}" 
    DisplayMemberPath="LocalizedValue" 
    SelectedValueMemberPath="AgeGroup" 
    Header="Age Group" 
    IsFilterable="True"/> 

But nothing is displayed in the column, only empty cells. However, when I click the Filter button in the header, all possible values are displayed, so it somehow knows the values. Not everything is wrong, it seems. :)

Any advice on this one?
Thanks.

5 Answers, 1 is accepted

Sort by
0
Andreas Bosch
Top achievements
Rank 1
answered on 19 Mar 2010, 01:49 PM
I have tried some other possibilities and finally (almost) came behind the logic of the properties. Now I use the following XAML to create the column:

<telerik:GridViewComboBoxColumn UniqueName="AgeGroup" 
    ItemsSource="{Binding AvailableAgeGroups}" 
    DataMemberBinding="{Binding AgeGroup.WrappedItem}" 
    SelectedValueMemberPath="WrappedItem" 
    DisplayMemberPath="LocalizedValue" 
    Header="Age Group" 
    IsFilterable="True"/> 

where the "WrappedItem" property contains the actual Enum value that is wrapped, i.e. it is another property of the EnumViewModel class, left out in the above post. Therefore, the EnumViewModel class looks like this:

public class EnumViewModel<TEnum>   
{   
    public object LocalizedValue { /* ... */ } 
 
    public TEnum WrappedItem { /* ... */ } 

Now, everything is displayed, and the ComboBoxes are filled with the correct values.

However, I still have one big problem: As already pointed out, I want the age group strings to be localized. This works well within the ComboBoxes - they display localized versions because the DisplayMemberPath is set to the LocalizedValue property which returns a localized string for the enum value. But if the cell is not in "edit mode" (i.e. no ComboBox displayed), the english (non-localized) version is displayed. I suppose this one is created by simply using the enum's ToString() method because the Bindings are set to the WrappedItem property which contains the plain enum value. This behavior is shown in the attached image.

Of course, I would like to display the localized version also when the cell is not in edit mode. From my understanding, I would have to change the DataMemberBinding or the SelectedValueMemberPath property for this to work. However, then the connection between these two properties would no longer match, so that the selection cannot be made correctly. Is this right? Which property do I have to set to achieve my desired result? Do I need to edit a CellTemplate?

Please help! Thanks!
0
Andreas Bosch
Top achievements
Rank 1
answered on 19 Mar 2010, 02:20 PM
OK, progress going on here... :)

I have further improved the result using the following XAML:

<telerik:GridViewComboBoxColumn UniqueName="AgeGroup" 
    ItemsSource="{Binding AvailableAgeGroups}" 
    DataMemberBinding="{Binding AgeGroup}" 
    DisplayMemberPath="LocalizedValue" 
    Header="Age Group" 
    IsFilterable="True"/> 

That is, I have removed the SelectedValueMemberPath (was "WrappedItem", i.e. enum value) and changed the DataMemberBinding from "AgeGroup.WrappedItem" to simply "AgeGroup". This way, the correct language is displayed in both cases: edit mode, and non-edit mode.

However, when I change the language at runtime (we use a custom Localization mechanism), the visible rows do not get updated. They keep displaying the old value, although the PropertyChanged event is raised for the "LocalizedValue" property (i.e. the DisplayMemberPath). When I scroll down in the GridView, all the other rows show the correct value because of virtualization, I suppose. When scrolling back, most of the previously wrong items now show the correct value, but some still do not.

Now when I raise a PropertyChanged event for the AgeGroup property in the PersonViewModel class after the language has been changed, the values are updated everywhere. It seems like, there is no real binding to the DisplayMemberPath, but only to the DataMemberBinding. Is there a way to have the value updated if PropertyChanged is raised for the property assigned to DisplayMemberPath? I cannot raise PropertyChanged in the PersonViewModel for every property when the language has changed. And AgeGroup is only one of the localized properties in the PersonViewModel, so please tell me if there is another solution.

Thanks a lot!

PS: I attached an image again, showing the current situation. Before the screenshot was made, I changed the language and then scrolled down some rows. The result is that the bottom rows display the correct values ("Erwachsen"), but the top rows still display the English value ("Adult"). When I select a cell, it is updated to the correct German value because it takes the value from the ComboBox, I suppose.
0
Accepted
Pavel Pavlov
Telerik team
answered on 19 Mar 2010, 03:23 PM
Hello Andreas Bosch,

Have you tried to call the RadGridView.Rebind() method after changing the language ?

Let me know in case this solution is not applicable .

Kind regards,
Pavel Pavlov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Andreas Bosch
Top achievements
Rank 1
answered on 19 Mar 2010, 04:13 PM
Thanks, this does help. However, it does not look like a really good solution to me. Maybe there is another way?!

I have also experienced two other problems now:

  1. I had several IndexOutOfRangeExceptions during scrolling and filtering. Unfortunately, I did not write down the location and cannot present a detailed report, but I will do so if it occurs again.
  2. If the auto-filter popup shows a vertical scrollbar (because there are many different items in there), I cannot move that scrollbar using the "thumb", I can only scroll by clicking on the arrow buttons or the space betweens buttons and thumb. Funny thing is, if I drag the thumb, it does not move, but instead the thumb of the main grid's scroll bar in the background moves!

Maybe you already had that problem and have any idea how to fix that?
0
Pavel Pavlov
Telerik team
answered on 22 Mar 2010, 04:32 PM
Hi Andreas Bosch,

About the problem with refreshing row content :
To force the UI To update you may raise the NotifyPropertyChanged for the property used in the DataMemberBinding. This is the only way to avoid rebinding the whole grid and refresh one row only.

About the two other problems .  I am currently trying to reproduce them here.  Any additional info or cod from your side that may help in reproducing the problem will be highly appreciated.

Regards,
Pavel Pavlov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
GridView
Asked by
Andreas Bosch
Top achievements
Rank 1
Answers by
Andreas Bosch
Top achievements
Rank 1
Pavel Pavlov
Telerik team
Share this question
or