c# - 访问 View 模型类中的选项卡控件

标签 c# wpf mvvm treeview tabcontrol

我有一个 View Model控制 Tree View .在我的 GUI 中还有一个 Tab Control不受 View Model 控制的.如果我想使用 Tree View访问 Tab Control 的元素我怎么给我的View Model访问它(Tab Control)?

我猜这与我如何从 Tree View 传递选定项目的方式类似。从代码隐藏到 View Model .

代码隐藏:

//Gets selected item in TreeView
private void Tree_One_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
       MainWindowViewModel.SelectedItem = e.NewValue as TreeViewItem;
}
View Model :
public static TreeViewItem SelectedItem { get; set; } //Selected Node\

我目前的尝试

代码隐藏:
MainWindowViewModel.middleControl = tabControl1 as TabControl;
View Model :
public static TabControl middleControl { get; set; }

以下内容不起作用,因为 View Model仍然看不到 Tab Control 中的任何项目
if (parent_Node.Items.Contains(SelectedItem))
                middleControl.SelectedIndex = nameOfTabItem;

最佳答案

View 模型不应直接使用 Tab 控件。 MVVM 架构的全部目的是分离 UI 和业务逻辑。只有 View 才能访问 UI 中的控件,即使如此,由于数据绑定(bind)和 View 模型中的逻辑,这也很少需要。

为什么选项卡控件不由 View 模型管理?

此外, View 模型不应该知道任何关于 TreeViewItems 的信息——它应该对绑定(bind)到树中每个项目的模型类进行操作。

关于c# - 访问 View 模型类中的选项卡控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18059790/

相关文章:

c# - The underlying connection 上的另一个线程已关闭。预期保持事件状态的连接已被服务器关闭

c# - 如何在 Android 使用内置 Material Design 时对 Xamarin.Forms iOS 应用程序进行样式化

c# - WPF应用程序框架IView问题

c# - 在 XAML 中将 ListBox 绑定(bind)到列表(集合)

c# - 如何在 ObservableCollection 构建的 View 中动态设置用户控件的 grid.column/row

c# - 在 Visual Studio 中解决 DisconnectedContext

c# - 用于提取方括号之间的值的正则表达式

wpf - 如何将 System.ServiceProcess 命名空间导入 WPF 项目?未在 CodeSense 中显示

.net - MVVM Light 如何判断哪些记录发生了变化

c# - MVVM 中的 Window.Closing 事件处理程序