关于c#:WPF Caliburn.Micro和TabControl-更改选项卡,而不更改模型

WPF Caliburn.Micro and TabControl - changing tab, not changing model

我是WPF的新手


好吧...
我已经使用过Caliburn.Micro,所以可以说我有一些经验,而不是专业人士,但是我设法使它起作用。

您的MainViewTestTabsViewModel.cs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 public interface IMainScreenTabItem : IScreen
    {
    }

    public class MainViewTestTabsViewModel : Conductor<IMainScreenTabItem>.Collection.OneActive
    {
        public MainViewTestTabsViewModel(IEnumerable<IMainScreenTabItem> tabs)
        {

            Items.Add(new ViewTabModel() {DisplayName ="Test"});
            Items.Add(new ViewTabModel() { DisplayName ="Test2" });
            Items.Add(new ViewTabModel() { DisplayName ="Test3" });
            Items.AddRange(tabs);
        }
    }

    public class ViewTabModel : Screen, IMainScreenTabItem
    {
        public ViewTabModel()
        {

        }
    }

和您的MainViewTestTabsView.xaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    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:local="clr-namespace:TestWpfApp.ViewModels"
    xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
   xmlns:viewModels="clr-namespace:TestWpfApp.Views"
    xmlns:cal="http://www.caliburnproject.org"
    mc:Ignorable="d" Width="500" Height="500">
    <Grid>
        <TabControl x:Name="Items">
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Label cal:Bind.ModelWithoutContext="{Binding}" x:Name="DisplayName" Height="200" Width="200"/>
                    </StackPanel>
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </Grid>
    </UserControl>

P.S。为什么我在构造函数中删除了displayName变量...因为您不需要它,所以它已经作为属性在Caliburn:Micro.Screen中。

编辑#2
该约定将起作用,只需在Label内添加cal:Bind.ModelWithoutContext="{Binding}"(已编辑答案)。


现在我有一些时间来测试您的示例项目...正如我评论的那样,您应该选择正确的绑定类型...

在"关于行动的所有方面"中,我想还??会有其他文档来源列出相同的基本信息:

  • Bind.Model – View-First - Set’s the Action.Target and DataContext properties to the specified instance. Applies conventions
    to the view. String values are used to resolve an instance from the
    IoC container. (Use on root nodes like Window/UserControl/Page.)
  • Bind.ModelWithoutContext - View-First - Set’s the Action.Target to the specified instance. Applies conventions to the view. (Use
    inside of DataTemplate.)
  • View.Model – ViewModel-First – Locates the view for the specified VM instance and injects it at the content site. Sets the VM
    to the Action.Target and the DataContext. Applies conventions to the
    view.

如前所述,我不是caliburn专家,所以我必须尝试一下...第二个选项对我来说似乎最好("在DataTemplate内部使用"),所以工作结果如下:<铅>

来自

1
<Label  cal:Bind.Model="{Binding}" x:Name="DisplayName" Height="200" Width="200" />

替换为

1
<Label cal:Bind.ModelWithoutContext="{Binding}" x:Name="DisplayName" Height="200" Width="200" />

及其工作。

实际上,我建议将模型引入周围的堆栈面板(数据模板根目录)中。

1
2
3
<StackPanel cal:Bind.ModelWithoutContext="{Binding}">
    <Label x:Name="DisplayName" Height="200" Width="200" />
</StackPanel>