首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未创建Catel ViewModel

未创建Catel ViewModel
EN

Stack Overflow用户
提问于 2014-02-01 11:12:04
回答 1查看 920关注 0票数 0

我有一个calel:Usercontrol,它将是一个带有过载GetViewModelType()方法的侧栏。

此外,我还有dependencyProperty来设置模型项。

问题是我的ViewModel从未初始化过。

我不知道为什么,但其他控件视图模型被正确初始化。

我就是这样做的:

在父视图中:

代码语言:javascript
复制
<itemSideBar:ItemSidebarView Grid.Column="1" PlaceItem="{Binding ElementName=itemsList, Path=SelectedItem}"></itemSideBar:ItemSidebarView>

我的sideBar控件codeBehind:

代码语言:javascript
复制
using Catel.Windows.Controls;

/// <summary>
/// Interaction logic for ItemSidebarView.xaml.
/// </summary>
public partial class ItemSidebarView : UserControl
{
    /// <summary>
    /// Initializes a new instance of the <see cref="ItemSidebarView"/> class.
    /// </summary>
    public ItemSidebarView()
    {
        InitializeComponent();

    }

    protected override Type GetViewModelType()
    {
        return typeof (ItemSideBarViewModel);
    }

    public static readonly DependencyProperty PlaceItemProperty = DependencyProperty.Register(
        "PlaceItem", typeof (PlaceItem), typeof (ItemSidebarView), new PropertyMetadata(default(PlaceItem)));

    public PlaceItem PlaceItem
    {
        get { return (PlaceItem) GetValue(PlaceItemProperty); }
        set { SetValue(PlaceItemProperty, value); }
    }
}

还有我的SideBarViewModel

代码语言:javascript
复制
using Catel.MVVM;

/// <summary>
/// UserControl view model.
/// </summary>
public class ItemSideBarViewModel : ViewModelBase
{
    public ItemSideBarViewModel()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="ItemSideBarViewModel"/> class.
    /// </summary>
    public ItemSideBarViewModel(PlaceItem item)
    {
        PlaceItem = item;
    }

    /// <summary>
    /// Gets the title of the view model.
    /// </summary>
    /// <value>The title.</value>
    public override string Title { get { return "View model title"; } }

    /// <summary>
    /// Gets or sets the property value.
    /// </summary>
    [Model]
    public PlaceItem PlaceItem
    {
        get { return GetValue<PlaceItem>(PlaceItemProperty); }
        set { SetValue(PlaceItemProperty, value); }
    }

    /// <summary>
    /// Register the PlaceItem property so it is known in the class.
    /// </summary>
    public static readonly PropertyData PlaceItemProperty = RegisterProperty("PlaceItem", typeof (PlaceItem), null);

    // TODO: Register models with the vmpropmodel codesnippet
    // TODO: Register view model properties with the vmprop or vmpropviewmodeltomodel codesnippets
    // TODO: Register commands with the vmcommand or vmcommandwithcanexecute codesnippets
}

你能解释一下应该怎么做吗?我对ListBox项和它的工作也做了同样的工作。

编辑:在日志中我可以看到:

代码语言:javascript
复制
12:37:59:829 => [DEBUG] [Catel.IoC.TypeFactory] Creating instance of type 'EliteCard.ViewModels.TabList.ItemSideBar.ItemSideBarViewModel' using specific parameters. No constructor found in the cache, so searching for the right one
12:37:59:830 => [DEBUG] [Catel.IoC.TypeFactory] Checking if constructor 'public ctor(PlaceItem place)' can be used
12:37:59:830 => [DEBUG] [Catel.IoC.TypeFactory] Constructor is not valid because value 'EliteCard.ViewModels.TabList.TabListViewModel' cannot be used for parameter 'EliteCard.ViewModels.TabList.TabListViewModel'
12:37:59:831 => [DEBUG] [Catel.IoC.TypeFactory] The constructor is valid and can be used
12:37:59:831 => [DEBUG] [Catel.IoC.TypeFactory] No constructor could be used, cannot construct type 'EliteCard.ViewModels.TabList.ItemSideBar.ItemSideBarViewModel' with the specified parameters
12:37:59:832 => [DEBUG] [Catel.IoC.TypeFactory] Creating instance of type 'EliteCard.ViewModels.TabList.ItemSideBar.ItemSideBarViewModel'. No constructor found in the cache, so searching for the right one.
12:37:59:832 => [DEBUG] [Catel.IoC.TypeFactory] Calling constructor.Invoke with the right parameters
12:37:59:834 => [DEBUG] [Catel.MVVM.ViewModelBase] Creating view model of type 'ItemSideBarViewModel' with unique identifier 3
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-01 11:59:15

您正在使用另一个视图中的视图。注意,Catel中的每个视图都有自己的视图模型。

正如您在日志中所看到的,它首先尝试参数最多的构造函数(这是使用模型注入)。之后,它将使用空构造函数。

在您的例子中,只需执行以下操作:

1)删除空构造函数(您不希望没有上下文的视图模型,对吗?)

2)确保设置有效的数据上下文。当前,数据上下文是父视图的上下文,父视图是父视图模型。在日志中,您可以看到,因为它说当前的数据上下文是TabListViewModel,而构造函数则需要PlaceItem类型的模型。

您可以像这样设置数据上下文:

代码语言:javascript
复制
<itemSideBar:ItemSidebarView Grid.Column="1" DataContext="{Binding ElementName=itemsList, Path=SelectedItem}" />

3)删除PlaceItem依赖项属性。这不是必需的。您只需从视图中的视图模型中获取模型(如果需要在代码隐藏中执行任何操作):

代码语言:javascript
复制
var vm = ViewModel as ItemSideBarViewModel;
if (vm != null)
{
    var myPlaceItem = vm.PlaceItem;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21497309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档