Why is Treeview, if the MVVM model inherits, extended in all tabs?

I don’t understand why, when I switch the extension of the tab tree to WPF, it then affects the extension of all kinds of table tree. I want each tab tree tree to be independent of each other. This is a very simple MVVM installation with several classes.

enter image description here

Here are the files from the project

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:data="clr-namespace:WpfApplication1"
        xmlns:view="clr-namespace:WpfApplication1.View"
        WindowStartupLocation="CenterScreen"
        Title="MainWindow" Height="350" Width="250">

    <Window.DataContext>
        <data:ViewModel/>
    </Window.DataContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--<Button Content="Add" Command="{Binding AddCommand}" Grid.Row="0"></Button>-->
        <TabControl x:Name="tabControl1" ItemsSource="{Binding TabItems}" Grid.Row="1" Background="LightBlue">
            <TabControl.ItemTemplate>
                <DataTemplate>
                        <TextBlock Text="{Binding Header}" VerticalAlignment="Center"/>
                </DataTemplate>
            </TabControl.ItemTemplate>

            <TabControl.ContentTemplate>
                <DataTemplate>
                    <view:TabItemView />
                </DataTemplate>
            </TabControl.ContentTemplate>


        </TabControl>

    </Grid>
</Window>

TabItemView.xaml

<UserControl x:Class="WpfApplication1.View.TabItemView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0" Text="{Binding Content}" />
        <TreeView Grid.Row="1" Background="Transparent">
            <TreeViewItem Header="Favorites">
                <TreeViewItem Header="USA"></TreeViewItem>
                <TreeViewItem Header="Canada"></TreeViewItem>
                <TreeViewItem Header="Mexico"></TreeViewItem>
            </TreeViewItem>
        </TreeView>
    </Grid>
</UserControl>

ViewModel.cs

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public class ViewModel
    {
        private ObservableCollection<TabItem> tabItems;

        public ObservableCollection<TabItem> TabItems
        {
            get { return tabItems ?? (tabItems = new ObservableCollection<TabItem>()); }
        }

        public ViewModel()
        {
            TabItems.Add(new TabItem { Header = DateTime.Now.ToString("Tab 1"), Content = DateTime.Now.ToString("F") });
            TabItems.Add(new TabItem { Header = DateTime.Now.ToString("Tab 2"), Content = DateTime.Now.ToString("F") });
            TabItems.Add(new TabItem { Header = DateTime.Now.ToString("Tab 3"), Content = DateTime.Now.ToString("F") });
        }
    }

    public class TabItem
    {
        public string Header { get; set; }
        public string Content { get; set; }
    }
}
+1
source share
1 answer

TabControl . , , (.. ), , . , , .

; , x:Shared="False" , Setter, Style, TabItem.

, TabItemTabItem WPF TabItem, , , , . x:Shared , TabControl.ContentTemplate .

:

  <TabControl.Resources>
    <DataTemplate x:Key="tabItemTemplate" x:Shared="False">
      <l:TabItemView />
    </DataTemplate>
    <s:Style TargetType="TabItem">
      <Setter Property="ContentTemplate" Value="{StaticResource ResourceKey=tabItemTemplate}"/>
    </s:Style>
  </TabControl.Resources>

: reset , .

, . , , , , , , . , bool , IsExpanded TreeViewItem:

:

public class TabItem
{
    public string Header { get; set; }
    public string Content { get; set; }
    public bool IsExpanded { get; set; }
}

:

<UserControl x:Class="TestSO33125188TreeViewTemplate.TabItemView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="auto"/>
      <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <TextBlock Grid.Row="0" Text="{Binding Content}" />
    <TreeView Grid.Row="1" Background="Transparent">
      <TreeViewItem Header="Favorites" IsExpanded="{Binding IsExpanded, Mode=TwoWay}">
        <TreeViewItem Header="USA"></TreeViewItem>
        <TreeViewItem Header="Canada"></TreeViewItem>
        <TreeViewItem Header="Mexico"></TreeViewItem>
      </TreeViewItem>
    </TreeView>
  </Grid>
</UserControl>

, Mode TwoWay, OneWay TreeViewItem.IsExpanded .

, , INotifyPropertyChanged . WPF , . INotifyPropertyChanged; , WPF , , , , .

0

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


All Articles