tags:

views:

23

answers:

2

I have a ComboBox hosted in a TabItem. When I select an item from the ComboBox, an appropriate ListView is populated. When I navigate away from the TabItem and then return, the SelectedItem in the ComboBox is empty, but the ListView remains populated correctly. The SelectedItemChanged event has not been raised.

Why is the selected item not shown in the ComboBox when I return to view it?

Some code: In the view ---

             <ComboBox x:Name="customersComboBox"
                              ItemsSource="{Binding Path=Customers }"
                              SelectedItem="{Binding Path=SelectedCustomer, UpdateSourceTrigger=PropertyChanged}"
                              DisplayMemberPath="Code"
                              IsEditable="False">

In the ViewModel -

    public ICustomerInfo SelectedCustomer
    {
        get { return (ICustomerInfo)GetValue(SelectedCustomerProperty); }
        set { SetValue(SelectedCustomerProperty, value); }
    }

    // Using a DependencyProperty as the backing store for SelectedCustomer.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SelectedCustomerProperty =
        DependencyProperty.Register("SelectedCustomer", typeof(ICustomerInfo), typeof(OrdersViewModel), new UIPropertyMetadata(null, SelectedCustomerChanged));

    private static void SelectedCustomerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d==null)
        {
            return;
        }
        OrdersViewModel viewModel = d as OrdersViewModel;
        if (e.NewValue == null)
        {
            return;
        }
        ICustomerInfo selectedCustomer = e.NewValue as ICustomerInfo;
        viewModel.SelectedCustomerChanged(selectedCustomer);
    }

    private void SelectedCustomerChanged(ICustomerInfo selectedCustomer)
    {
         if (selectedCustomer != null)
        {
            if (!GetOrders())
            {
                return;
            }
        }
    }
A: 

set EnabledViewState=true.... and post some code.... so that i can recognize the error

Sheetal Inani
This is a WPF issue.
hayrob
A: 

I've worked out what was happening.

When the Tab loses focus, the SelectedItemChanged event IS raised! I think I understand the mechanism that causes the event to be raised, but I don't understand why it needs to happen - apparently it is "by design".

What was happening is that the e.NewValue was null and my code did not change to the new values but the SelectedItem WAS set to null.

Programming error but the strange behaviour of the TabItem (and its child controls) had me flumoxed!

hayrob