绑定不适用于MVVM中的用户控件

时间:2014-05-22 13:35:12

标签: mvvm

此tabcontrol出现在我的主窗口

      <TabControl ItemsSource="{Binding MyTabItem}"
                  SelectedItem="{Binding SelectedTab}">
                   <TabControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Header}"/>
                        </DataTemplate>
                   </TabControl.ItemTemplate>
                    <TabControl.ContentTemplate>
                        <DataTemplate>
                            <uc:TabItemControl/>
                        </DataTemplate>
                    </TabControl.ContentTemplate>
                       </TabControl>

这是我的主视模型:

命名空间iDeploy_MVVM.ViewModel {    公共类MainViewModel:NotifyObjectBase     {

   private DataAccessLayer da;
   private AITInfo selectedAIT;
   private ComponentInfo selectedComponent;
   private TabItemViewModel selectedTab;
   public MainViewModel()
   {
       da = new DataAccessLayer();
       this.Title = Common.iDeploy + Properties.Settings.Default.Environment;
       this.AITs =  da.getAITNames().Rows.Cast<DataRow>().Select(a=> 
           new AITInfo()
           {
               AITId = Convert.ToInt32(a["AITID"].ToString()) ,
               AITName = a["AITName"].ToString() 
           }).ToList();

       this.SelectedAIT = this.AITs[0];

   }

   public ComponentInfo SelectedComponent
   {
       get { return selectedComponent; }
       set
       {
          selectedComponent = value;
           OnPropertyChanged("SelectedComponent");
           selectedComponent.Tab = da.getApp(selectedComponent.ComponentId).Rows.Cast<DataRow>()
                                          .Select(t =>
                                              new TabItemViewModel(
            da.getAppRelease(Convert.ToInt32(t["AppID"].ToString())).Rows.Cast<DataRow>()
                                                  .Select(ar => new AppReleaseInfo()
                                                  {
                              AppReleaseId = Convert.ToInt32(ar["AppReleaseID"].ToString()),
                              ReleaseName = ar["ReleaseName"].ToString(),
                              ReleaseRooPath = ar["ReleaseRootPath"].ToString()
                                                }).ToList(),
                                      new AppInfo()
                                         {
                                         AppId = Convert.ToInt32(t["AppID"].ToString()),
                                         TechnologyName = t["TechnologyName"].ToString()
                                                  })).ToList();

           this.MyTabItem = selectedComponent.Tab;
           if(this.MyTabItem.Count>0)
           OnPropertyChanged("MyTabItem");
       }
   }

      public ICollection<TabItemViewModel> MyTabItem { get; set; }

     }}

在主视图中更改组合框的值时,将触发selectedcomponent,并创建所需数量的tabitems实例,并在每个实例中加载usercontrol。

我已经设置了usercontrol datacontext                                

此组合框存在于usercontrol

           <ComboBox ItemsSource="{Binding AppReleases}" DisplayMemberPath="ReleaseName" 
              SelectedValuePath="AppReleaseId"
              Style="{StaticResource CustomComboBoxStyle}"/>

这是我的usercontrol的viewmodel

    private AppReleaseInfo selectedApp;
    public TabItemViewModel(ICollection<AppReleaseInfo> appRelease, AppInfo app)
    {
        this.Header = app.TechnologyName;
        this.AppReleases = appRelease.Select(a=>a).ToList();
        OnPropertyChanged("AppReleases");
    }

    public TabItemViewModel()
    {

    }

    public string Header { get; set; }


    public IList<AppReleaseInfo> AppReleases { get; set; }

    public AppReleaseInfo SelectedApp
    {
        get { return selectedApp; }
        set
        {
            selectedApp = value;
            OnPropertyChanged("SelectedApp");

        }
      }

    }

现在,Appreleases正在检索所需的所有值,并且通过ReleaseName,我需要在usercontrol组合框中显示名称。在调试时,我可以看到Appreleases中的数据,但是usercontrol中的绑定不起作用。但是,tabitem Header绑定工作正常。

请告诉我这里缺少的东西。我是否需要执行任何额外步骤以确保usercontrol xaml文件中定义的绑定正常工作?

0 个答案:

没有答案