我有一个 calel:Usercontrol ,它将是一个带有覆盖 GetViewModelType() 方法的侧边栏。
另外我有dependencyProperty 来设置模型项。
问题是我的 ViewModel 从未初始化。
我不知道为什么,但其他控件视图模型已正确初始化。
我就是这样做的:
在父视图中:
<itemSideBar:ItemSidebarView Grid.Column="1" PlaceItem="{Binding ElementName=itemsList, Path=SelectedItem}"></itemSideBar:ItemSidebarView>
我的侧边栏控制代码在后面:
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
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 项目及其工作做了同样的事情。
编辑:在日志中我可以看到:
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