gpt4 book ai didi

c# - 在选项卡项中为 View 设置数据上下文

转载 作者:行者123 更新时间:2023-12-03 10:37:37 26 4
gpt4 key购买 nike

在我的窗口资源中,我有一个用于我的viewmodel的数据模板和一个静态的mainviewmodel

<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项目中
SharedSettingsViewModelTabList.Add(new Shared.Models.TabItem() { Header = "Sql Databases", Content = DatabaseViewModel });

如何将 DatabaseView的数据上下文设置为 DatabaseViewModel内部的 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");
}
}
}

最佳答案

您将需要更新以下代码,并且DatabaseViewModel将成为DataContextDatabaseView,这将是DataTemplateContentContentPresenterContentTemplateTabControl

       <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>

关于c# - 在选项卡项中为 View 设置数据上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19466981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com