Binding is lost when using TabControl and MVVM

I still consider myself a newbie when it comes to MVVM, and I have a binding problem in TabControl. My application allows the user to create countries and states, enter some information, and then save them in a database. The following is a description of the structure of my application:

The underlying ViewModel / View is called ApplicationViewModel / ApplicationView. The ApplicationViewModel has an ObservableCollection called tabs consisting of one AllNationsViewModel and one AllStatesViewModel. This Tabs property is bound to the Items control of the TabControl in the ApplicationView.

AllNationsViewModel / AllNationsView are used to display all countries that have been entered by the user. It also allows the user to create new countries and select a specific country for a more thorough study. AllStatesViewModel / AllStatesView do the same, but for states.

Finally, I have a NationViewModel / NationsView that is specific to a nation; there is also a StateViewModel / StateView that does the same for state.

For a nation, you can currently enter only a name, but for a state, you can enter a name, as well as the country in which it is a member. The nation is selected using the ComboBox, which displays all the nations created on the "Countries" tab.

I am using the static DataFacade class as an interface to my data warehouse. You can add, delete and retrieve a list of countries / states using this interface; it also fires events when something is added or removed.

The problem I am facing is that when there is a state in the AllStatesViewModel object (CurrentStateViewModel property) and I go to the countries tab and then back to the states tab, the current selected state has lost its country. All other states are still in order.

I will try to show the corresponding code below (I removed the unnecessary code from some methods):

Status Class:

class State
{
   public string Name { get; set; }

   public Nation Nation { get; set; }
}

TabControl in ApplicationView:

<TabControl ItemsSource="{Binding Tabs}" Margin="6">
   <TabControl.ItemTemplate>
      <DataTemplate>
         <ContentPresenter Content="{Binding DisplayName}" />
      </DataTemplate>
   </TabControl.ItemTemplate>
   <TabControl.ContentTemplate>
      <DataTemplate>
         <ContentPresenter Content="{Binding }" />
      </DataTemplate>
   </TabControl.ContentTemplate>
</TabControl>

When the user creates the nation, AddCommand from AllNationsViewModel is launched:

private void Add(NationViewModel vm)
{
   DataFacade.AddNation(vm.Nation);
}

AllStatesViewModel receives a notification when a country is added to the data warehouse:

private void OnDataStoreNationsChanged(object sender, DataFacadeEventArgs e)
{
   Nations.Add(new NationViewModel(e.Context as Nation));
}

- ObservableCollection of NationViewModels. ComboBox StateView , / :

<ComboBox Grid.Row="1" Grid.Column="1" SelectedValue="{Binding Nation}" SelectedValuePath="Nation" ItemsSource="{Binding DataContext.Nations, RelativeSource={RelativeSource AncestorType={x:Type local:AllStatesView}}}">
   <ComboBox.ItemTemplate>
      <DataTemplate>
         <TextBlock Text="{Binding Name}" />
      </DataTemplate>
   </ComboBox.ItemTemplate>
</ComboBox>

, . , Nations AllStatesViewModel, ApplicationViewModel, . , AllStatesView TabControl, , Nation StateViewModel null? , , .

-, , , ? . , , MVVM- .

EDIT: :

Atm :

AllNationsView, atm NationView - "" .

Of nations

AllStatesView, ( StateView). , , "", "", . , .

States

+4
1

WPF . , , .

. . , . , .

- ApplicationViewModel ApplicationViewModel . RelativeSource .

+2

Source: https://habr.com/ru/post/1629381/


All Articles