views:

563

answers:

6

Hi,

I have my silverlight app which pulls data into a datagrid from a view model. The vm is exposed via Mef. I also have a details grid which has comboboxes. The vm also contains the data to populate the combobox values. Upon first load, everything works fine and the selected items on te comboboxes are correct and I can select alternative values. However, if I sort my main data grid (allow sort=true) then I find the binding for selected value on the comboboxes dissapear. The combobox is still populated with data but nothing is selected.

Has anyone come across this issue before? I am unsure how to solve this one.

Thanks

A: 

Anyone have any ideas about this? Still haven't managed to solve it. thanks

Shaggy
A: 

How are you gathering the Data for the combobox? Is it a list of strings or a list of specific objects? What could be occuring is that the sorting is creating another set of objects within its combobox, or each row of data, and the selected item is no longer matching the reference. Could you post a code example?

Agies
A: 

Code for the comboboxes is as follows

 <TextBlock>Status</TextBlock>
        <ComboBox x:Name="CB_Status"   ItemsSource="{Binding Status}" SelectedValuePath="StatusId" SelectedValue="{Binding CurrentCall.StatusId, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" ItemTemplate="{StaticResource StatusTemplate}" />
        <TextBlock>Priority</TextBlock>
        <ComboBox x:Name="CB_Priority"  ItemsSource="{Binding Priorities}" SelectedValuePath="PriorityId" SelectedValue="{Binding CurrentCall.PriorityId, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" ItemTemplate="{StaticResource PriorityTemplate}"/>
        <TextBlock>Issue Type</TextBlock>
        <ComboBox x:Name="CB_IssueType" ItemsSource="{Binding IssueType}" SelectedValuePath="IssueTypeId" SelectedValue="{Binding CurrentCall.IssueTypeId, Mode=TwoWay,NotifyOnValidationError=True, ValidatesOnExceptions=True}" ItemTemplate="{StaticResource IssueTemplate}" />

The data is pulled from a VM, and the data is called using async calls at the start, and the variables are populated as follow:

   private IEnumerable<Priority> _priorities;

    public IEnumerable<Priority> Priorities
    {
        get { return _priorities; }
        set
        {
            if (value != _priorities)
            {
                _priorities = value;
                this.RaisePropertyChanged("Priorities");
            }
        }
    }
 private IEnumerable<Status> _status;

    public IEnumerable<Status> Status
    {
        get { return _status; }
        set
        {
            if (value != _status)
            {
                _status = value;
                this.RaisePropertyChanged("Status");
            }
        }
    }


    private IEnumerable<IssueType> _issueType;

    public IEnumerable<IssueType> IssueType
    {
        get { return _issueType; }
        set
        {
            if (value != _issueType)
            {
                _issueType = value;
                this.RaisePropertyChanged("IssueType");
            }
        }
    }

so comboboxes are IEnumerable collections of various entities. The thing is upon sorting, the parent table, the combo boxes lose their selected value, but the data for the combobox remains intact. Via fiddler I can see that there arent any subsequent calls to fetch the data for the comboboxes.

Shaggy
A: 

One thought and issue I have had with SelectedValue before, is that when a combox, datagrid, etc... go through state changes like; Loss of focus, Redrawing, and a few other they will change the SelectedValue to null. It is possible, that when you choose a value the SelectedValue (bound properties) on the VM are set. However, when the Grid sorts it also tells the VM to set the SelectedValue to 'null'. So, after the sort the comboboxes are set to default values.

A thing you can try, is set a breakpoint at one of the SelectedValue properties 'set' and see how often the value is set, during Debug.Assert if the value is null.

Agies
A: 

Hi,

Not sure of your setup here, but if your datagrid is a list of calls and CurrentCall is the selected item can you not use Element Binding? E.g.

<ComboBox x:Name="CB_Status"   
                      ItemsSource="{Binding Status}" 
                     SelectedItem="{ Path=SelectedItem.Status, Mode=TwoWay, ElementName=YOUR_DATAGRID}" 
                     ItemTemplate="{StaticResource StatusTemplate}" /> 

I assume the grid’s datacontext is bound to IEnumerable<Call> (or something) on your VM so I’d say a sort would result in a new collection (like if you said .Sort or order etc).

Here is a quick cut from a working example (using a listbox not datagrid in this case)

<ComboBox 
   DisplayMemberPath="DisplayName"
   SelectedItem="{Binding Path=SelectedItem.Individual.IndividualNameTitle, 
                   Mode=TwoWay, ElementName=AccountList}"
   ItemsSource="{Binding Path=IndividualNameTitles}">
</ComboBox>

Hope it helps.

Chooksii
A: 

Shaggy, I just noticed this the other day trying to setup async ComboBox loading. For some reason the ComboBox just appears to drop the binding (but you already knew that). Anyway, I put this post together that addresses some of these issues. Let me know if it helps.

http://blogs.msdn.com/kylemc/archive/2010/06/18/combobox-sample-for-ria-services.aspx

Kyle

Kyle McClellan