I do not even know how to correctly describe the title.

XAML available:

<TabControl ItemsSource="{Binding AnimeLists}"> <TabControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding URStatusRus}" /> </DataTemplate> </TabControl.ItemTemplate> <TabControl.ContentTemplate> <DataTemplate> <ListView ItemsSource="{Binding List}" SelectedItem="{Binding SelectedView}"><!-- нужное свойство SelectedView находится в корне VM класса, а XAML ищет внутри свойства AnimeLists[i] --> <ListView.View> <GridView> <GridView.Columns> <GridViewColumn Header="Название" DisplayMemberBinding="{Binding Name}" /> <GridViewColumn Header="Эпизоды" DisplayMemberBinding="{Binding Progress}" /> <GridViewColumn Header="Оценка" DisplayMemberBinding="{Binding Score}" /> <GridViewColumn Header="Тип" DisplayMemberBinding="{Binding Kind}" /> </GridView.Columns> </GridView> </ListView.View> </ListView> </DataTemplate> </TabControl.ContentTemplate> </TabControl> 

AnimeLists is a collection of five objects (one per tab). The object has 2 properties. One for the tab title, the other for the list. The list is transferred to the ListView, which displays it in accordance with the template.

Do not forget that the tabs 5, and hence the ListView also 5.

Now I want to bind the SelectedItem to a property in the VM. If I add a property to the AnimeLists class, then the SelectedView property (as I called the property in the VM) will be 5. And in order to link it with another control in another part of the interface, I need to know the AnimeLists index (that is, the active tab index).

Therefore, I decided to try to place my SelectedView property in the root of the class so that it receives the last selected item regardless of the open tab, but the dilemma is that the code does not see the fields and properties "above" the AnimeLists [i] property.

In this connection, the question is whether inside the template it is possible to somehow bind a SelectItem to a property higher in the hierarchy than AnimeLists [i]. (i is the index of the active tab).

Or maybe there is some other elegant solution so that I have one property related to the selected element?

  • one
    Give the name Window or UserControl - what do you have there root? And then you can bind through this name, for example for the button: <Button Command="{Binding DataContext.AddSynonymAsNewWordCommand, ElementName=Root}" , where Root is the name given for UserControl, which houses ItemsControl - Bulson
  • My @Bulson DataContext is set dynamically in App.xaml.cs. The class that is given in the DataContext of the window is MainVM. var mainWindow = new MainWindow() { DataContext = mainVM }; mainWindow.Show(); something like this: I launch the main window from App.xaml.cs - MrModest
  • one
    And that's fine :) In the mainVM create the SelectedAnime property and then you can link like this: DataContext.SelectedAnime, ElementName=Root - Bulson
  • @Bulson exactly Root? - MrModest
  • one
    Root is the name given in XAML for the window x:Name="Root" you can name as you want. - Bulson

0