切换选项卡后WPF ComboBox选择更改
我基于嵌套选项卡创建了一个项目。 嵌套选项卡是相同viemModel和相同UI的不同实例。 当我在标签之间切换时,标签中存在的combobox会根据失去焦点的标签来改变选择。
我添加了viewmodels和我的测试项目的视图。 预先感谢您的帮助
主窗口
查看模型(每次创建不同的istance)
class MainWindowViewModel : WorkspacesViewModel { public MainWindowViewModel() { this.WorkspacesView.CurrentChanged += new EventHandler(WorkspacesView_CurrentChanged); } void WorkspacesView_CurrentChanged(object sender, EventArgs e) { } RelayCommand myVar = null; public ICommand AddCommand { get { return myVar ?? (myVar = new RelayCommand(param => { SetWindow(new IntermediateViewModel("AA" + this.Workspaces.Count) ); })); } }
第一级标签
第一级视图模型
class IntermediateViewModel:WorkspacesViewModel {public string Header {get; 组; }
public IntermediateViewModel(string header) { Header = header; SetWindow(new ClassViewModel(header)); } }
嵌套选项卡
嵌套标签视图模型
public enum myTypes { tipo0 = 0, tipo1 = 1, tipo2 = 2, } class ClassViewModel : WorkspaceViewModel { public ClassViewModel(string name) { Name = name; } public string Name { get; set; } private List _statusList = null; public List StatusList { get { if (_statusList == null) _statusList = new List() { new IntEnumType((int)myTypes.tipo0, myTypes.tipo0.ToString()), new IntEnumType((int)myTypes.tipo1, myTypes.tipo1.ToString()), new IntEnumType((int)myTypes.tipo2, myTypes.tipo2.ToString()), }; return _statusList; } } private int myVar = 1; public int MyProperty { get { return myVar; } set { if (myVar != value) { myVar = value; OnPropertyChanged(() => MyProperty); } } } } public class TabItemStyleSelector : StyleSelector { public Style MainTabItem { get; set; } public Style ChildrenTabItem { get; set; } public Style SpecificationTabItem { get; set; } public override Style SelectStyle(object item, DependencyObject container) { //if (item is IHome) // return MainTabItem; //else if (item is SpecificationItemViewModel) // return SpecificationTabItem; //else return ChildrenTabItem; } }
代码有点难以完全遵循,但我猜测问题是你的ClassViewModel
只有一个实例,它是存储combobox选择的地方{Binding Path=MyProperty
,所以无论存储什么在MyProperty
中,无论它们在哪里,它都会反映在combobox的所有实例中。
问题出在您加载的事件处理程序中。
切换选项卡时,卸载一个选项卡并加载一个新选项卡。
我怀疑你在_spl2Status_Loaded
更改了_spl2Status_Loaded
。