我是那种新手的& MVVM &卡利所以请你原谅:)WPF Caliburn.Micro和TabControl的 - 改变选项卡,在WPF不改变模型
我有一个问题与动态创建的模型结合的TabControl 。自定义的Tabcontrol被正确创建,但改变标签不切换用来绑定“视图”视图模型(我使用的视图模型第一种方法)
我做了我的解决方案立足于这样一个问题:WPF Caliburn.Micro and TabControl with UserControls issue
这是我的模型定义:
public interface IMainScreenTabItem : IScreen
{
}
public class MainViewTestTabsViewModel : Conductor<IMainScreenTabItem>.Collection.OneActive
{
public MainViewTestTabsViewModel(IEnumerable<IMainScreenTabItem> tabs)
{
Items.Add(new ViewTabModel("Foo1"));
Items.Add(new ViewTabModel("Foo2"));
Items.AddRange(tabs);
}
}
public sealed class ViewTabModel : Screen, IMainScreenTabItem
{
public ViewTabModel(string displayName)
{
DisplayName = displayName;
}
}
这里是视图MainViewTestTabsView:
<UserControl x:Class="TestWpfApp.Views.MainViewTestTabsView"
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.Views"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:viewModels="clr-namespace:TestWpfApp.ViewModels"
xmlns:cal="http://www.caliburnproject.org"
mc:Ignorable="d" Width="500" Height="500">
<Grid>
<TabControl Name="Items">
<TabControl.ContentTemplate>
<DataTemplate>
<StackPanel>
<Label cal:Bind.Model="{Binding}" x:Name="DisplayName" Height="200" Width="200" />
</StackPanel>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
我想实现的目标是让TabControl带有许多选项卡。每个选项卡具有相同的“视图”(在DataTemplate中声明),但绑定此视图我想使用不同的viewModels(具体 - 相同的模型类[ViewTabModel],但具有不同的数据)
选项卡的大小是在运行时声明以及应该在ViewTabModel模型中的数据。
在下面的例子 - 我有两个标签,但改变他们是不会改变的标签(我把所有的时间:“Foo1”标签,即使我点击“foo2的”标签)
我用卡利。微观作为框架 - 与autofac bootstrap(如果它很重要) 而我使用propertyChanged.Fody(https://github.com/Fody/PropertyChanged)省略所有在viewmodels中的属性changed stuff。
我在做什么错?
=== UPDATE ===
附加最小再现解决方案:
https://wetransfer.com/downloads/0b909bfd31a588dda99655f366eddad420170801192103/1d094a
普莱舍,救命啊! :)
===更新2 ===
什么不清楚关于我的问题?:)不过没有意见,没有与它赏金anwsers事件。
===更新3 ===
我已经发布完整视图页面(XAML)和完整的模型代码(这仅仅是这个)
我还张贴AppBoostraper.cs和AppWindowManager.cs(但我想这是irrelevat这里)
AppBoostrapper.cs
using Autofac;
using TestWpfApp.ViewModels;
namespace TestWpfApp {
using System;
using System.Collections.Generic;
using Caliburn.Micro;
public class AppBootstrapper : CaliburnMetroAutofacBootstrapper<MainViewTestTabsViewModel>
{
protected override void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterType<AppWindowManager>().As<IWindowManager>().SingleInstance();
var assembly = typeof(ShellViewModel).Assembly;
builder.RegisterAssemblyTypes(assembly)
.Where(item => item.Name.EndsWith("ViewModel") && item.IsAbstract == false)
.AsSelf()
.SingleInstance();
}
}
}
它继承CaliburnMetroAutofacContainer(https://github.com/ziyasal/Caliburn.Metro)
AppWindowsManager。CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Caliburn.Metro.Core;
using MahApps.Metro.Controls;
namespace TestWpfApp
{
public class AppWindowManager : MetroWindowManager
{
public override MetroWindow CreateCustomWindow(object view, bool windowIsView)
{
if (windowIsView)
{
return view as ShellView;
}
return new ShellView
{
Content = view
};
}
}
}
=== UPDATE 4 === Apprently,从改变控制:
CAL:Bind.Model = “{结合}” ×:名称= “DisplayName的”
到:
内容= “{结合DisplayName的}”
做了工作。虽然我不太清楚为什么?
现在我想要做的完全一样。只有这一次,我希望我的看法被束缚。所以ViewModel完全一样。但是,这一次:
<UserControl x:Class="TestWpfApp.Views.MainViewTestTabsView"
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.Views"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:viewModels="clr-namespace:TestWpfApp.ViewModels"
xmlns:cal="http://www.caliburnproject.org"
mc:Ignorable="d" Width="500" Height="500">
<Grid>
<TabControl Name="Items">
<TabControl.ContentTemplate>
<DataTemplate>
<StackPanel>
<local:ViewTab cal:Bind.Model="{Binding}" />
</StackPanel>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
和ViewTab视图
MainViewTestTabsView是:
<UserControl x:Class="TestWpfApp.Views.ViewTab"
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.Views"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:viewModels="clr-namespace:TestWpfApp.ViewModels"
xmlns:cal="http://www.caliburnproject.org"
mc:Ignorable="d" Width="300" Height="300">
<Grid>
<StackPanel>
<Label x:Name="DisplayName"></Label>
</StackPanel>
</Grid>
===更新5 =>幸福的最后=== 我已被消化我应该坚持ViewModel的第一次会议(正如我宣称的那样使用)和我的尝试是somhow首先查看。所以,我把它改为:
<ContentControl cal:View.Model="{Binding ActiveItem}" />
但没有什么是呈现然后
我若这样的:
<ContentControl cal:View.Model="{Binding}" />
有只说消息:“对于无法找到的看法: [my_namspece] .ViewTabModel 这很奇怪,让我思考,也许我不会坚持这个惯例,而且这是真的......
我的模特叫:
ViewTabModel
而应该是:
ViewTabViewModel
正好与观点同样的事情。它应该被称为:
ViewTabView.xaml
之后,这样的结构:
<ContentControl cal:View.Model="{Binding}" />
工作正常!谢谢arcticwhite和grek40领导我这种解决方案
是否缺少结合的模式? – Ramankingdom
我用Caliburn很久回来。你有没有尝试ActivateItem(viewmodel在这里)? – Ramankingdom
关于变化的标签,你需要激活视图使用ActivateItem – Ramankingdom