控制不同的DataContext [英] Control different DataContext

查看:209
本文介绍了控制不同的DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

控件在WPF不同的DataContext



正如我可以部署在不同的标签和控制,是当前的DataContext


$ B $多个的DataContext b



我M使用的 MVVM光WPF4 我有不同的ViewModels,看法,但我不知道如何处理倍数的DataContext和控制目前的DataContext改变标签切换


$ b $上b

编辑:



我有一个办法解决方案如下:




  1. 为的MainView

  2. 创建一个视图模型
  3. 的TabControl的来源是一个的ObservableCollection

  4. 每个TabItem的都有自己的DataContext

  5. 菜单上有DataContext的是这样的:的DataContext ={绑定路径= CurrentTab.DataContext}时,在视图模型添加新的TabItem的<哪里CurrentTab改变/ LI>


我有以下问题:




  1. 我怎么从TabControl的连接视图模型当您更改标签



解决方案:这个问题是这MVVM光使用ViewModelLocator在静态的方式绑定视图模型,这是问题当我添加标签在C#中ViewModelLocator不工作,在其他的方式,我需要装载手动视图模型对于这样每个标签:

  //在MainModelView.cs 

公共RelayCommand MyCommand {搞定;组; }

私人无效RegisterCommand()
{
MyCommand =新RelayCommand(()=>
{
AddTab(标签头,新TabViewModel(),新TabContentControl());
});
}

私人无效AddTab(串头,对象上下文,ContentControl中的内容)
{
TabItem的= NULL;

的foreach(在TabItemList TabItem的标签)
{
如果(tab.Header.Equals(头);
{
TabItem的标签=;
}
}

如果(空== TabItem的)
{
TabItem的=新TabItem的();
tabItem.Header =头;
tabItem.Content =内容;
tabItem.DataContext =背景;
TabItemList.Add(TabItem的);
}

CurrentTabIndex = TabItemList.IndexOf(TabItem的);
}

2,不要DataContext的更新菜单,我的代码是错误的?



解决方案


$ b $:前一个点与解决的跟踪代码解决这个也是一样,只b

  //在RegisterCommands()
ChangeTabCommand =新RelayCommand< TabItem的>(标签=>
{
如果(空==标签)返回;
CurrentTabContext = tab.DataContext;
}

在主窗口。 XML:

 <  -  MainWindow.xaml  - > 

<按钮内容=NewTab命令={绑定路径= MyCommand}/>

< TabControl的
保证金=5 5 5 0
Grid.Row =1
的ItemsSource ={绑定路径= TabItemList}
的SelectedIndex ={绑定路径= CurrentTabItemIndex}
X:NAME =工作区>
< I:Interaction.Triggers>
< I:的EventTrigger事件名称=的SelectionChanged>
< CMD:EventToCommand
命令={结合ChangeTabCommand}
CommandParameter ={结合的SelectedItem,的ElementName =工作区}/>
< /我:&的EventTrigger GT;
< /我:Interaction.Triggers>
< / TabControl的>



编辑2:




  1. 如何避免修改在视图模型视图,并从同一个视图(ContenControl,头,上下文)


解决方案

我要创建具有为每个视图模型的财产ViewModelContainer(如MainViewModel,Tab1ViewModel,Tab2ViewModel)。
中可以绑定ViewModelContainer作为窗口的DataContext并且每个TabItem的DataContext的结合权VM对象以这种方式的DataContext ={结合Tab1ViewModel}



不建议对问题2。



更新



您的代码不遵循MVVM在100%。您的命令编辑视图,视图模型。如果你想成为艰苦的命令必须只与视图模型进行互动。然后视图模型将信号(通过一个ObservableCollection或INotifyPropertyChanged接口),将通过增加一个新TabItem的回复的图。
我想,查看部分可使用的ItemTemplate来定义面板应如何显示管理100%的XAML。


Control different DataContext in WPF

As I can deploy multiple DataContext in different tabs and control which is the current DataContext

I'm using Mvvm Light WPF4 i have the different ViewModels, View but i dont know how to handle multiples DataContext and control the current DataContext for change on tab switch

Edit:

I've got an approach to the solution as follows:

  1. Create a ViewModel for the MainView
  2. The tabcontrol source is a ObservableCollection
  3. Each TabItem has its own DataContext
  4. The menu has the DataContext like this: DataContext="{Binding Path=CurrentTab.DataContext}" where CurrentTab change when add new TabItem in the ViewModel

i have the following problems:

  1. how do I connect the ViewModel from the TabControl when you change the tab?

Solution: the problem is that Mvvm Light uses a ViewModelLocator for Binding ViewModel in static way, this is the problem when i add tab in C# the ViewModelLocator dont works, in other way i need load manually the ViewModel for each tab like this:

// in MainModelView.cs

public RelayCommand MyCommand { get; set; }

private void RegisterCommand()
{
  MyCommand = new RelayCommand(() =>
  {
    AddTab("Tab Header", new TabViewModel(), new TabContentControl());
  });
}

private void AddTab(string header, object context, ContentControl content)
{
  TabItem = null;

  foreach(TabItem tab in TabItemList)
  {
    if(tab.Header.Equals(header);
    {
      tabItem = tab;
    }
  }

    if(null == tabItem)
    {
      tabItem = new TabItem();
      tabItem.Header = header;
      tabItem.Content = content;
      tabItem.DataContext = context;
      TabItemList.Add(tabItem);
    }

    CurrentTabIndex = TabItemList.IndexOf(tabItem);    
}

2.the DataContext dont update in the menu, my code is wrong?

Solution: the previous point solve this too and only with the follow code solved:

// in RegisterCommands()
ChangeTabCommand = new RelayCommand<TabItem>(tab =>
{
  if (null == tab) return;
  CurrentTabContext = tab.DataContext;
}

in MainWindow.xml:

  <!-- MainWindow.xaml -->

  <Button Content="NewTab" Command="{Binding Path=MyCommand }" />

  <TabControl
      Margin="5 5 5 0"
      Grid.Row="1"
      ItemsSource="{Binding Path=TabItemList}"
      SelectedIndex="{Binding Path=CurrentTabItemIndex}"
      x:Name="Workspace">
      <i:Interaction.Triggers>
          <i:EventTrigger EventName="SelectionChanged">
              <cmd:EventToCommand
              Command="{Binding ChangeTabCommand }"
              CommandParameter="{Binding SelectedItem, ElementName=Workspace}"/>
          </i:EventTrigger>
      </i:Interaction.Triggers>
  </TabControl>    

Edit 2:

  1. How to avoid modifying the view in the ViewModel and send the necessary parameters from the same view (ContenControl, Header, Context)

解决方案

I would create a ViewModelContainer that has a property for each of your view models (e.g. MainViewModel, Tab1ViewModel, Tab2ViewModel). The you can bind ViewModelContainer as DataContext of the Window and bind each TabItem DataContext to the right VM object in this way DataContext="{Binding Tab1ViewModel}"

No suggestion for problem 2.

Update

Your code does not follow MVVM at 100%. Your command edit the view and viewmodel. If you want to be painstaking the command must interact only with viewmodel. The viewmodel then will signal (through an ObservableCollection or an INotifyPropertyChanged interface) the view that will reply by adding a new tabItem. I suppose that the View part can be managed 100% with XAML using ItemTemplate to define how the panels should appear.

这篇关于控制不同的DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆