1

在我的窗口资源中,我有一个用于视图模型的数据模板和一个静态主视图模型

<Window.Resources>
        <DataTemplate DataType="{x:Type SharedViewModels:DatabaseViewModel}">
            <SharedViews:DatabaseView/>
        </DataTemplate>

        <LocalViewModels:SharedSettingsViewModel x:Key="SharedSettingsViewModel"/>
</Window.Resources>

DatabaseViewModel是我静态中的一个实例,我SharedSettingsViewModel 有这个选项卡控件

<TabControl DataContext="{Binding Source={StaticResource SharedSettingsViewModel}}" ItemsSource="{Binding SharedSettingsViewModelsTabList}">
                        <TabControl.ItemTemplate>
                            <DataTemplate>
                                <ContentPresenter Content="{Binding Content}"/>
                            </DataTemplate>
                        </TabControl.ItemTemplate>
                    </TabControl>

我像这样将项目添加到我的 TabControl itemssorce

SharedSettingsViewModelTabList.Add(new Shared.Models.TabItem() { Header = "Sql Databases", Content = DatabaseViewModel });

如何将 my 的数据上下文设置为 my内部DatabaseViewDatabaseViewModel实例SharedSettingsViewModel

在我看来 DatabaseView XAML 这是我尝试过的用户控件

DataContext="{Binding DatabaseViewModel}"

但这似乎不起作用

这是我的 TabItem 模型

public class TabItem : MVVM.ObservableObject
    {
        public string Header { get; set; }
        public MVVM.ObservableObject Content { get; set; }
    }

我的 DatabaseViewModel 将被定义为 SharedSettingsViewModel 中的一个属性

 private ObservableCollection<Models.Database> databases;

 public ObservableCollection<Models.Database> Databases
        {
            get
            {
                if (databases == null)
                {
                    databases = new ObservableCollection<Models.Database>();
                    databases.Add(new Models.Database() { Displayname = "new" });
                }
                return databases;
            }
            set
            {
                SetField(ref databases, value, "Databases");
            }
        }

这是我的 SharedSettingsViewModel ,其中包含选项卡项集合

public class SharedSettingsViewModel : MVVM.ObservableObject 
    {
    private ObservableCollection<Shared.Models.TabItem> SharedSettingsViewModelTabList;
    public ObservableCollection<Shared.Models.TabItem> SharedSettingsViewModelTabList
            {
                get
                {
                    if (sharedSettingsViewModelTabList == null)
                    {
                        sharedSettingsViewModelTabList = new ObservableCollection<Shared.Models.TabItem>();
                    }
                    return sharedSettingsViewModelTabList;
                }

            }

    //my DatabaseViewModel property..as a child view model

            private Shared.ViewModels.DatabaseViewModel databaseViewModel;
            public Shared.ViewModels.DatabaseViewModel DatabaseViewModel
            {
                get
                {
                    if (databaseViewModel == null)
                    {
                        databaseViewModel = new Shared.ViewModels.DatabaseViewModel();
                    }
                    return databaseViewModel;
                }
                set
                {
                    SetField(ref databaseViewModel, value, "DatabaseViewModel");
                }
            }
}
4

1 回答 1

1

您将必须更新代码如下,您DatabaseViewModel将成为DataContext您的,DatabaseView这将DataTemplate是您ContentContentPresenterContentTemplateTabControl

       <TabControlDataContext="{Binding Source={StaticResource SharedSettingsViewModel}}" ItemsSource="{Binding SharedSettingsViewModelsTabList}">
            <TabControl.ContentTemplate>
                   <DataTemplate>
                        <ContentPresenter Content="{Binding Content}"/>
                    </DataTemplate>
            </TabControl.ContentTemplate>
            <TabControl.ItemContainerStyle>
                <Style TargetType="TabItem">
                    <Setter Property="Header" Value="{Binding Header}"/>
                </Style>
            </TabControl.ItemContainerStyle>
        </TabControl>
于 2013-10-19T15:25:06.813 回答