使用 mahapps 和 prism 绑定选项卡控件 - WPF

Binding tab controls with mahapps and prism - WPF

我正在使用 mahappsprism[模块化] 构建 WPF 应用程序。我有以下 HomeWindow.xaml 代码。

<Controls:MetroWindow x:Class="Project.Views.HomeWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:local="clr-namespace:Project.Views"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True" 
        <!--The above code is for automatically binding of viewmodel into view-->
        Height="700" Width="1200" Background="White">
    <Grid>
        <TabControl ItemsSource="{Binding TabCollection}">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock>                            
                        <TextBlock Text="{Binding Name}"/>
                    </TextBlock>
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <Label Content="{Binding Content}" />
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </Grid>
</Controls:MetroWindow>

我在 ViewModels 目录下的 HomeViewModel.cs 中有以下结构。

public class HomeViewModel : BindableBase
{
    private ObservableCollection<Item> _tabCollection;
    public ObservableCollection<Item> TabCollection { get { return _tabCollection; } set { SetProperty(ref _tabCollection, value); } }
    //Prism way of getting and setting data
}

public class Item
{
    private string Name;
    private string Content;

    public Item(string name, string content)
    {
        Name = name;
        Content = content;
    }
}

下面是我如何通过 HomeWindow.xaml.cs.

将数据添加到 TabCollection 属性
private HomeViewModel _model=new HomeViewModel();
public HomeWindow(EmployeeViewModel model)
{
    InitializeComponent();
    _model.UserViewModel = model;
    LoadHomeData(_model.UserViewModel.EmpRole);
    DataContext = this;
}

private void LoadHomeData(string Role)
{
    if (string.Equals(Role, "Admin"))
    {
        _model.TabCollection= new ObservableCollection<Item>()
        {
            new Item("Test1", "1"),
            new Item("Test2", "2"),
            new Item("Test3", "3")
        };
    }
}

无论如何,标签页都不会显示。它是一片空白 window。我遵循了 issue here 中的示例,并浏览了一些采用相同方法的类似帖子。但其中 none 有帮助。这是因为 databindingprism 方式还是这里还缺少其他任何内容?希望能找到一些帮助..

您的问题与 MahApps 或 Prism 无关,而是与 WPF 的一般工作方式有关。在您的情况下 NameContent 是私有字段,应该是 public properties

public string Name { get; set; }
public string Content { get; set; }

private 或 field 不是有效的绑定源。您可以在 Binding Sources Overview 下找到有关什么是有效绑定源的更多信息,但就您的情况而言,就 CLR 对象而言:

You can bind to public properties, sub-properties, as well as indexers, of any common language runtime (CLR) object. The binding engine uses CLR reflection to get the values of the properties. Alternatively, objects that implement ICustomTypeDescriptor or have a registered TypeDescriptionProvider also work with the binding engine.

还有一个问题就是DataContext设置错了。目前设置为 HomeWindow,我认为它应该设置为 HomeViewModel 的实例,其中包含 TabCollection 属性

DataContext = _model;