Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit RC0 ComboBox in DataGrid Issue
24 replies. Latest Post by reka on October 14, 2009.
(0)
manish.d...
Member
99 points
21 Posts
09-26-2008 4:42 PM |
When a ComboBox control is used inside a DataGridTemplateColumn.CellEditingTemplate, it shows odd behavior. Sometimes the dropdown works, however majority of times, it just flashes(When you click, it opens, and then closes immediately). I have tried this on two machines and can reproduce the same behavior.
Here is the code to help reproduce
Page.xaml
<UserControl x:Class="Silverlight.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" xmlns:src="clr-namespace:Silverlight" Width="400" Height="300"> <UserControl.Resources> <src:CityProvider x:Key="cityProvider"/> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White"> <data:DataGrid x:Name="dataGrid" AutoGenerateColumns="False"> <data:DataGrid.Columns> <data:DataGridTemplateColumn Header="City"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding City}" /> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> <data:DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <ComboBox SelectedItem="{Binding City, Mode=TwoWay}" ItemsSource="{Binding CityList, Source={StaticResource cityProvider}}" /> </DataTemplate> </data:DataGridTemplateColumn.CellEditingTemplate> </data:DataGridTemplateColumn> </data:DataGrid.Columns> </data:DataGrid> </Grid> </UserControl>
Page.xaml.cs
namespace Silverlight { public partial class Page : UserControl { public Page() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Page_Loaded); } void Page_Loaded(object sender, RoutedEventArgs e) { this.dataGrid.ItemsSource = new List<CityInfo>() { new CityInfo() { City="City 1"}, new CityInfo() { City="City 2"}, new CityInfo() { City="City 3"}, }; } } public class CityInfo { public string City { get; set; } } public class CityProvider { public List<string> CityList { get { return new List<string> { "City 1", "City 2", "City 3", "City 4" }; } } } }
hcanfly
18 points
13 Posts
09-28-2008 1:51 AM |
I have the same problem, only I have to work very hard to get the popup to ever stay up. My only difference is that I have different items sources for the datagrid and the combobox.
-Gary
09-28-2008 2:18 AM |
Also, it seems to work fine if it's inside a CellTemplate, but not inside a CellEditingTemplate
brauliod
Participant
1169 points
472 Posts
09-28-2008 10:12 AM |
I have managed to crash my application using a combo inside a Popup.
Quite disgusting, I was eager to replace my listBox with a Combo, but get bad crashes the second time I open the popup ... rolled back to listBox version :-(((((((((.
wackyphill
37 points
119 Posts
09-28-2008 5:34 PM |
I saw something like this too. I had a usercontrol in the popup. The user control had a comboBox.
I believe the problem was the usercontrol's Loaded event fires everytime the popup is shown, yet the UserControl is kept in memory from the first time it was popped up.
So if you set say the ItemsCollection of the CombBox in the Loaded event you get a problem.
So I did this in my Loaded Event:
if( myCombo.ItemsSource == null) { myCombo.ItemsSource = MyCollection; }
This made it only happen once and no more problems. Not sure what the correct behavior should be.
A bigger concern I have is that the ComboBox has no SelectedValue property like it does in WPF. I don't know how to bind a record to the ComboBox so it will show the correct Item from its ItemsCollection.
lee_sl
Contributor
2992 points
585 Posts
09-28-2008 7:54 PM |
Here is a workaround. (Manish I took the code from you post)
http://leeontech.wordpress.com/2008/09/28/combobox-in-datagrid-issue-with-rc0/
09-28-2008 7:59 PM |
wackyphill,
I noticed missing SelectedValue and SelectedValuePath as well, it is a requirement in business applications, in majority of cases where foreign keys are used. However there are couple of work abounds. Simplest one is to add new property with same type as that of ComboBox and bind selected item to it.
For instance if your business class is about Item and you have CategoryId as FK, and in ComboBox you are showing Category description, you can modify Item class with new property called CategoryItem that will return same type that you are binding in ComboBox(CategoryId and Description). Once you have that in place, bind SelectedItem property of ComboBox to that new property CategoryItem on Item and you are all set.
I will have a blog post explaining the whole process with sample code soon...
http://weblogs.asp.net/manishdalal/
09-28-2008 8:05 PM |
lee_sl,
I just tested the workaround and it works!
Thanks a lot! Hoepfully SL2 RTM will resolve the core issue.
09-29-2008 8:47 AM |
Manish,
Thanks for the idea. But I think in my case I'll either end up handling the the selectionChanged event manually or something because I don't really want to replace what should be an interger in my data class with an object. It fixes the ComboBx shortcoming but I think it will make using the data class more difficult in other places.
It's a shame the value property is missing here when every other version of a ComboBox in the .NET world has one.
Thanks again.
09-29-2008 10:19 AM |
You do not have to change integer to object. I have just blogged about the process. It is quite simple, just add a new property. Here is the post
http://weblogs.asp.net/manishdalal/archive/2008/09/28/combobox-in-datagrid.aspx
Hopefully that hepls solve the issue.
09-29-2008 1:03 PM |
I see, I had misunderstood what you meant.
Hopefully the ComboBox will be improved so it is not necessary to extend every class you wish to use with a ComboBox.
But that is an interesting work around.
Thank you for sharing it.
09-30-2008 2:26 AM |
Thanks a lot, I will give a try. It's a pity that we had waited so much for a combo, and control deployed is not well unit tested.
Brian Br...
368 points
66 Posts
12-19-2008 9:22 PM |
The above DataGrid bug (where the ComboBox closes immediately) has been addressed in the DataGrid December 2008 Release. The version released with Silverlight 2 would not work correctly with editing controls that opened Popups (like ComboBox or DatePicker). Opening the Popup would cause focus to leave the DataGrid, a control which automatically ends edit on LostFocus, thus preventing users from manipulating its contents.
12-20-2008 8:16 AM |
Good news that they have fixed the bug... bad news the fix breaks the current workaround :-(
Mmmm.... That this applies to the SDK? I mean the user will use the same lugin, but if I just download the latest SDK it will get updated isn't it?
Thanks
Braulio
12-20-2008 11:49 PM |
I just tested by remvoing the workaround and ComboBox now works properly. Also the bug with changing ComboBox list size seems to have been fixed as well! It now displays properly evertime list changes!
12-21-2008 3:34 AM |
Wow !! That's excellent news. But the thing of the combo size happened as well outside the DataGrid (I mean the dropdown box size), It's there an update for the combo control as well?
yifung
3313 points
540 Posts
12-23-2008 3:06 PM |
Unfortunately, the fix was part of a mini release for the DataGrid only. Please see our announcement for details. Happy holidays
05-02-2009 5:44 PM |
I know that the fix on the combo were only applied to the DataGrid, but did anybody found other workarounds ?
Now I'm display dynamically a ComboBox inside a Grid control, but whenever I open the dropdown of the combo the LostFocus of that control is fired, does that makes sense? Is there any workaround?
thanks
05-02-2009 7:21 PM |
I agree that that does not make sense.
What you can do is when the you get the LostFocus, get the newly focused element from the FocusManager. If this element is a popup and its Parent is the ComboBox then ignore the event.
05-02-2009 8:14 PM |
Thanks Yifung,
Right now I'm trying to use the combo inside a Grid container... finally what I did is combine: LostFocus, with GotFocus on almost all the controls that I can, and even hiding the combo when you change the selection... by doing all this sort of combinations I almost manage to simulate a "LostFocus" working... (changed selection, hide combobox box, show a textblock).
I'm quite concerned about the Combobox, when we were talking about Beta 2 or even RC or early RTW, I can understand that it had so many bugs, but half year has passed and it's still an unstable component (and no way to convince a client to avoid using a combobox in their UI :-)).
Do you have plans to replace or fix this control on SL3?
05-03-2009 7:07 PM |
I've opened a bug on the ComboBox so the owners can address this issue. Thanks for bringing this to our attention
reka
58 points
29 Posts
09-15-2009 9:46 AM |
hi
i am using silverlight3RTM with .net ria service
i want know how to get data in combobox in datagrid
from database in a foreign key
can any one tell any example
09-15-2009 9:59 AM |
The best approach is to use declarative binding, in the selected item for sure you are using something like an Integer value to identify the item. I would use this combobox extension (try to define it inside your datatemplate):
http://www.lhotka.net/weblog/SilverlightComboBoxControlAndDataBinding.aspx
Good luck
09-16-2009 6:51 AM |
In this example not match my application bcoz my combox had in selecteditem not display selected value
i am using silverlight3RTM with .net ria july
i am using three tables with foreignkey
so how to bind in combo box in datagrid and dataform one table in a particular field ????
so any one tell how to binding combox in datagrid and combobox in dataform ???
10-14-2009 4:05 AM |
any example for datagrid combobox selectionchanged event??
i choose selected item is ID but display member path is Name.
so i choose any name in datagrid combobox column only put in ID
How is it possible??