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