Skip to main content

Microsoft Silverlight

Answered Question RC0 ComboBox in DataGrid IssueRSS Feed

(0)

manish.dalal
manish.d...

Member

Member

99 points

21 Posts

RC0 ComboBox in DataGrid Issue

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" };
            }
        }
    }
}

 

 

Manish Dalal
http://feeds2.feedburner.com/manishdalal

hcanfly
hcanfly

Member

Member

18 points

13 Posts

Re: RC0 ComboBox in DataGrid Issue

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

 

hcanfly
hcanfly

Member

Member

18 points

13 Posts

Re: RC0 ComboBox in DataGrid Issue

Also, it seems to work fine if it's inside a CellTemplate, but not inside a CellEditingTemplate

-Gary

 

brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

Re: Re: RC0 ComboBox in DataGrid Issue

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 :-(((((((((.

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

wackyphill
wackyphill

Member

Member

37 points

119 Posts

Re: Re: RC0 ComboBox in DataGrid Issue

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
lee_sl

Contributor

Contributor

2992 points

585 Posts

Answered Question

Re: Re: RC0 ComboBox in DataGrid Issue

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/

----------------------------------------------
Available for consulting in Dallas, TX
http://leeontech.wordpress.com/

manish.dalal
manish.d...

Member

Member

99 points

21 Posts

Re: Re: RC0 ComboBox in DataGrid Issue

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/

Manish Dalal
http://feeds2.feedburner.com/manishdalal

manish.dalal
manish.d...

Member

Member

99 points

21 Posts

Re: Re: RC0 ComboBox in DataGrid Issue

lee_sl,

I just tested the workaround and it works!

Thanks a lot! Hoepfully SL2 RTM will resolve the core issue.

Manish Dalal
http://feeds2.feedburner.com/manishdalal

wackyphill
wackyphill

Member

Member

37 points

119 Posts

Re: Re: RC0 ComboBox in DataGrid Issue

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.

 

manish.dalal
manish.d...

Member

Member

99 points

21 Posts

Re: Re: RC0 ComboBox in DataGrid Issue

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.

Manish Dalal
http://feeds2.feedburner.com/manishdalal

wackyphill
wackyphill

Member

Member

37 points

119 Posts

Re: Re: RC0 ComboBox in DataGrid Issue

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.

brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

Re: Re: RC0 ComboBox in DataGrid Issue

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.

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

Brian Braeckel
Brian Br...

Member

Member

368 points

66 Posts

Re: RC0 ComboBox in DataGrid Issue

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.

brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

Re: RC0 ComboBox in DataGrid Issue

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

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

manish.dalal
manish.d...

Member

Member

99 points

21 Posts

Re: RC0 ComboBox in DataGrid Issue

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!

Manish Dalal
http://feeds2.feedburner.com/manishdalal

brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

Re: Re: RC0 ComboBox in DataGrid Issue

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?

 

Thanks

  Braulio

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

yifung
yifung

Contributor

Contributor

3313 points

540 Posts

Microsoft

Re: Re: RC0 ComboBox in DataGrid Issue

Unfortunately, the fix was part of a mini release for the DataGrid only. Please see our announcement for details.  Happy holidays

Yifung Lin [MSFT]

brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

Re: Re: Re: RC0 ComboBox in DataGrid Issue

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

   Braulio

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

yifung
yifung

Contributor

Contributor

3313 points

540 Posts

Microsoft

Re: Re: Re: RC0 ComboBox in DataGrid Issue

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.

Yifung Lin [MSFT]

brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

Re: Re: Re: Re: RC0 ComboBox in DataGrid Issue

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?

  Thanks

    Braulio

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

yifung
yifung

Contributor

Contributor

3313 points

540 Posts

Microsoft

Re: Re: Re: Re: RC0 ComboBox in DataGrid Issue

I've opened a bug on the ComboBox so the owners can address this issue.  Thanks for bringing this to our attention

Yifung Lin [MSFT]

reka
reka

Member

Member

58 points

29 Posts

Re: RC0 ComboBox in DataGrid Issue

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

Remember to click "Mark as Answer" on the post that helps U
Thanks and Regards
By
REKA

brauliod
brauliod

Participant

Participant

1169 points

472 Posts

Silverlight MVP

Re: Re: RC0 ComboBox in DataGrid Issue

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

  Braulio 

 

// ---------------------------------
    Braulio Diez

    http://www.dbschemaeditor.com
    Free Silverlight Based Database Schema Editor
/// ---------------------------------

reka
reka

Member

Member

58 points

29 Posts

Re: Re: Re: RC0 ComboBox in DataGrid Issue

http://www.lhotka.net/weblog/SilverlightComboBoxControlAndDataBinding.aspx

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 ???

 

Remember to click "Mark as Answer" on the post that helps U
Thanks and Regards
By
REKA

reka
reka

Member

Member

58 points

29 Posts

Re: Re: Re: RC0 ComboBox in DataGrid Issue

hi

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??

 

Remember to click "Mark as Answer" on the post that helps U
Thanks and Regards
By
REKA
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities