Catel MVVM xctk:PropertyGrid =错误

时间:2013-07-04 18:36:06

标签: mvvm xceed catel

当我将Catel Framework与Xceed.Wpf.Toolkit.PropertyGrid一起使用时出错。 错误在于,如果我从ViewModelBase继承,则PropertyGrid是不可见的自定义属性 如果我从ModelBase继承,那一切都是正常的

这段代码很好用

    public class PersonViewModel : ModelBase
{
    [DisplayName(@"Название")]
    [Description(@"Название стратегии")]
    [Category(@"Основные")]
    [PropertyOrder(0)]
    public string Person
    {
        get { return GetValue<string>(PersonProperty); }
        set { SetValue(PersonProperty, value); }
    }

    public static readonly PropertyData PersonProperty = RegisterProperty("Person", typeof(string));
}

但此代码无效

    public class PersonViewModel : ViewModelBase
{
    [DisplayName(@"Название")]
    [Description(@"Название стратегии")]
    [Category(@"Основные")]
    [PropertyOrder(0)]
    public string Person
    {
        get { return GetValue<string>(PersonProperty); }
        set { SetValue(PersonProperty, value); }
    }

    public static readonly PropertyData PersonProperty = RegisterProperty("Person", typeof(string));
}

XAML

 <xcad:LayoutAnchorable ContentId="alarms"
                                               Title="Alarms"
                                               >
                            <xctk:PropertyGrid BorderThickness="0"
                                               SelectedObject="{Binding Path=SelectedObject}"
                                               ShowSearchBox="False"
                                               ShowSortOptions="False"
                                               Width="Auto"
                                               AutoGenerateProperties="False"
                                               NameColumnWidth="150">
                                <xctk:PropertyGrid.PropertyDefinitions>
                                    <xctk:PropertyDefinition Name="Person" />
                                </xctk:PropertyGrid.PropertyDefinitions>
                            </xctk:PropertyGrid>
                        </xcad:LayoutAnchorable>

1 个答案:

答案 0 :(得分:1)

使用视图模型时,向其添加视图很重要。您已创建PersonViewModel,但没有PersonView。

如果您不想为Person创建单独的视图,则不需要PersonViewModel。我们认为在视图模型中创建子视图模型不是正确的方法。这就是我们在Catel中创建嵌套用户控件解决方案的原因。

这里有2个选项:

  1. 创建一个自定义PersonView(它将动态地与 PersonViewModel)
  2. 保留PersonModel(就是这样,一个人的模型)