在WPF中,如果我在窗口';上运行以下应用程序,将选择两个选项卡;s 10机器

在WPF中,如果我在窗口';上运行以下应用程序,将选择两个选项卡;s 10机器,wpf,.net-4.6,Wpf,.net 4.6,若我在window的7系统上运行低于应用程序,它可以正常工作,但若我在window的10机器上运行它,则在单击“添加”按钮后会选择两个选项卡“选项卡1”和“选项卡2”。Windows10安装了.NET4.6框架。我再也无法选择“表1” namespace TabControlTest { using System.Collections.Generic; using System.Collections.ObjectModel; using System.Componen

若我在window的7系统上运行低于应用程序,它可以正常工作,但若我在window的10机器上运行它,则在单击“添加”按钮后会选择两个选项卡“选项卡1”和“选项卡2”。Windows10安装了.NET4.6框架。我再也无法选择“表1”

namespace TabControlTest
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Windows.Input;


    public  class MainWindowViewModel :INotifyPropertyChanged
    {
        private TabItem _selectedItem;

        private TabItem _homePageTabItem;

        private ObservableCollection<TabItem> _tabItems;
          private IList<TabItem> _tabBackingList;

        public MainWindowViewModel()
        {
             _homePageTabItem = new TabItem
                                       {
                                           Header = "Tab 1",
                                           Content = "Tab 1 Content",
                                           IsSelected = true,
                                           IsChecked = true
                                       };


             _tabBackingList = new List<TabItem> { _homePageTabItem };

            TabItems = new ObservableCollection<TabItem>(_tabBackingList);
            AddCommand = new RelayCommand(AddTabs);
        }

        public ICommand AddCommand { get; private set; }

        public ObservableCollection<TabItem> TabItems
        {
            get
            {
                return _tabItems;
            }

            set
            {
                if (_tabItems != value)
                {
                    _tabItems = value;
                    OnPropertyChanged();
                }
            }
        }

        public TabItem SelectedItem
        {
            get
            {
                return _selectedItem;
            }
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;
                    OnPropertyChanged();
                }
            }
        }

        private void AddTabs(object param)
        {
              _tabBackingList.Clear();
              _tabBackingList.Add(_homePageTabItem);

             var item2 = new TabItem { Header = "Tab 2", Content = "Tab 2 Content", IsSelected = true, IsChecked = true };
            var item3 = new TabItem { Header = "Tab 3", Content = "Tab 3 Content", IsSelected = false };
            _tabBackingList.Add(item2);
            _tabBackingList.Add(item3);
            TabItems = new ObservableCollection<TabItem>(_tabBackingList);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}


namespace TabControlTest
{
    using System;
    using System.Windows.Input;

    public class RelayCommand : ICommand
    {
        private readonly Predicate<object> _canExecute;
        private readonly Action<object> _execute;

        public RelayCommand(Action<object> execute)
        {
            _execute = execute;
        }

        public bool CanExecute(object parameter)
        {
            if (_canExecute == null) return true;

            return _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        public event EventHandler CanExecuteChanged;
    }
}

namespace TabControlTest
{
    using System.ComponentModel;
    using System.Runtime.CompilerServices;

    public class TabItem : INotifyPropertyChanged
    {
        private bool _isSelected;

        private bool _isChecked;
        public string Header { get; set; }
        public string Content { get; set; }

        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }

            set
            {
                if (_isSelected != value)
                {
                    _isSelected = value;
                    OnPropertyChanged();
                }
            }
        }


        public bool IsChecked
        {
            get
            {
                return _isChecked;
            }

            set
            {
                if (_isChecked != value)
                {
                    _isChecked = value;
                    OnPropertyChanged();
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

namespace TabControlTest
{
    using System.Windows;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel();
        }
    }
}

<Window x:Class="TabControlTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tabControlTest="clr-namespace:TabControlTest"
                     xmlns:uxc="http://schemas.thermofisher.com/2013/UXLibrary/Controls"

        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Button Content="Add" Margin="5" Grid.Row="0" Width="100" HorizontalAlignment="Left" Command="{Binding AddCommand}" ></Button>
        <TabControl  Margin="5" Grid.Row="1" SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding TabItems}" >
            <TabControl.ItemContainerStyle>
                <Style TargetType="TabItem">
                    <Setter Property="Header" Value="{Binding Header}"/>
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"></Setter>
                </Style>
            </TabControl.ItemContainerStyle>
            <TabControl.ContentTemplate>
                <DataTemplate DataType="{x:Type tabControlTest:TabItem}">
                    <Button Content="{Binding Content}"></Button>
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </Grid>
</Window>
namespace选项卡控制测试
{
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Runtime.CompilerServices;
使用System.Windows.Input;
公共类MainWindowViewModel:INotifyPropertyChanged
{
私有选项卡项_selectedItem;
private TabItem\u homePageTabItem;
私人可观察收集项目;
私有IList_禁忌列表;
公共主窗口视图模型()
{
_homePageTabItem=新建TabItem
{
Header=“表1”,
Content=“表1内容”,
IsSelected=true,
IsChecked=true
};
_tabBackingList=新列表{u homePageTabItem};
TabItems=新的ObservableCollection(\u tabBackingList);
AddCommand=新的RelayCommand(AddTabs);
}
公共ICommand AddCommand{get;private set;}
公共可观测集合选项卡项
{
得到
{
返回禁忌项;
}
设置
{
如果(_tabItems!=值)
{
_tabItems=值;
OnPropertyChanged();
}
}
}
公共选项卡项SelectedItem
{
得到
{
返回_selectedItem;
}
设置
{
如果(_selectedItem!=值)
{
_选择editem=值;
OnPropertyChanged();
}
}
}
私有void AddTabs(对象参数)
{
_tabBackingList.Clear();
_tabBackingList.Add(_homePageTabItem);
var item2=new TabItem{Header=“Tab 2”,Content=“Tab 2 Content”,IsSelected=true,IsChecked=true};
var item3=new TabItem{Header=“Tab 3”,Content=“Tab 3 Content”,IsSelected=false};
_tabBackingList.Add(第2项);
_tabBackingList.Add(第3项);
TabItems=新的ObservableCollection(\u tabBackingList);
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
var handler=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}
命名空间选项卡控制测试
{
使用制度;
使用System.Windows.Input;
公共类中继命令:ICommand
{
私有只读谓词_canExecute;
私有只读操作\u执行;
公共中继命令(操作执行)
{
_执行=执行;
}
公共布尔CanExecute(对象参数)
{
if(_canExecute==null)返回true;
返回_canExecute(参数);
}
public void Execute(对象参数)
{
_执行(参数);
}
公共事件处理程序CanExecuteChanged;
}
}
命名空间选项卡控制测试
{
使用系统组件模型;
使用System.Runtime.CompilerServices;
公共类选项卡项:INotifyPropertyChanged
{
私立学校当选;
检查私人住宅;
公共字符串头{get;set;}
公共字符串内容{get;set;}
公选学校
{
得到
{
返回(未选);;
}
设置
{
如果(_isSelected!=值)
{
_isSelected=值;
OnPropertyChanged();
}
}
}
公共场所被检查
{
得到
{
返回-已检查;
}
设置
{
如果(_已检查!=值)
{
_isChecked=值;
OnPropertyChanged();
}
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
var handler=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}
命名空间选项卡控制测试
{
使用System.Windows;
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
DataContext=新的MainWindowViewModel();
}
}
}

我不确定windows7中的行为,但它应该会产生相同的错误。 正如您在本实施中看到的,您正在通过以下方式替换ICommand中的整个选项卡源:

TabItems = new ObservableCollection<TabItem>(_tabBackingList);
TabItems=新的ObservableCollection(\u tabBackingList);
因此,这意味着您的“Tab1”实例不存在,预期UI不会得到更新。
 var item2 = new TabItem { Header = "Tab 2", Content = "Tab 2 Content", IsSelected = true, IsChecked = true };
 var item3 = new TabItem { Header = "Tab 3", Content = "Tab 3 Content", IsSelected = false };

 //_tabBackingList.Add(item2);
 //_tabBackingList.Add(item3);
 //TabItems = new ObservableCollection<TabItem>(_tabBackingList);

 TabItems.Add(item2);
 TabItems.Add(item3);