This project is read-only.

Strange behavior in RowDetailsTemplate

Topics: Bug Report Forum, Developer Forum
Nov 30, 2008 at 11:10 PM
Edited Dec 1, 2008 at 3:00 AM
Hi im using DataGrid with a RowDetailsTemplate, and i have a combobox here whose items i get from a collection and the SelectedValue from another collection property so it changes depending of the current item in the DG, however when i clear the items bound to the grid and reload them with updated items, the combobox here no longer points to the correct item in the source collection, i have checked on the RowDetailsVisibilityChanged event and i can see the combobox hold the correct item in CurrentItem property but not in the SelectedItem property (its null) so what i do in this event is find by name the combobox in the DetailsElement of the event and restore the SelectedItem with the value of CurrentItem and that's it however i think there should be a better way to do this, anyone?

this is the code in the handler

        void _GridSolicitudes_RowDetailsVisibilityChanged(object sender, Microsoft.Windows.Controls.DataGridRowDetailsEventArgs e)
        {
            ComboBox cb = (e.DetailsElement as Grid).FindName("SolicitanteCombo") as ComboBox;
            cb.SelectedItem = cb.Items.CurrentItem;
        }

and this is the code of the combo, by the way both collections (combo's itemssource and datagrid's items source are observablecollections<>)

ComboBox x:Name="SolicitanteCombo" ItemsSource="{Binding Source={x:Static models:DCSolicitudesDisenoModel.Usuarios}, Mode=OneWay}"
                                                               SelectedValue="{Binding UsuarioVentasSolicitaID}"
                                                               DisplayMemberPath="UserName" SelectedValuePath="ID" IsEnabled="False" IsReadOnly="True"></ComboBox>

i have tried with IsSynchronizedWithCurrentItem = true but no luck i get null always on selecteditem any ideas? thanks

Edit: i have discovered thats not actually the solution since currentItem is always the first item in the combo's collection :( any ideas?
Dec 1, 2008 at 3:33 AM
Finally i found a not so clean way that is so obscure i have never ever seen before and is using BindingOperations class to update the binding at SelectedValue of the combo like this:

        void _GridSolicitudes_RowDetailsVisibilityChanged(object sender, Microsoft.Windows.Controls.DataGridRowDetailsEventArgs e)
        {
            ComboBox cb = (e.DetailsElement as Grid).FindName("SolicitanteCombo") as ComboBox;
            BindingOperations.GetBindingExpressionBase(cb, ComboBox.SelectedValueProperty).UpdateTarget();
        }

I hope someone else points a better way to do it automagically just like when it first binds to both the itemssource and selectedvalue collections