wpf - WPF设计时间数据困惑

标签 wpf mvvm blendability

为什么当我为MVVM View 创建一些设计时数据时,我必须嵌套两次xmlns才能访问该属性的属性?

例子:

       <d:DesignProperties.DataContext>
            <local:UserListViewModel>
                <local:UserListViewModel.Users xmlns:local="clr-namespace:ChatModule.ViewModels">
                    <local:User Name="Person 1" />
                    <local:User Name="Person 2" />
                    <local:User Name="Person 3" />
                    <local:User Name="Person 4" />
                    <local:User Name="Person 5" />
                    <local:User Name="Person 6" />
                </local:UserListViewModel.Users>
            </local:UserListViewModel>
       </d:DesignProperties.DataContext>

我已经在UserControl的xmlns中定义了一个本地属性。但是要访问我的基础类中的User结构,我必须在Users属性中再次定义它。

这是为什么?

所有这些工作正常……我可以看到很棒的运行时数据。我以前从未做过,因此能够看到它很整洁。我注意到我经常会得到错误,这些错误在我编译时就会消失...但是只要我更改xaml就会再次出现。
The property 'Name' was not found in type 'User'.
The property 'Name' was not found in type 'User'.
The property 'Name' was not found in type 'User'.
The property 'Name' was not found in type 'User'.
The property 'Name' was not found in type 'User'.
The property 'Name' was not found in type 'User'.

显然可以,否则我将无法看到运行时数据。

我还想将设计时间数据提取到另一个xaml类中,并在此类中引用它,因此我没有实际的设计时间数据位于 View 本身内……仅仅是对另一个xaml文件的引用。

所以我这样做了...像这样...
<UserListViewModel xmlns="clr-namespace:ChatModule.ViewModels">
    <UserListViewModel.Users xmlns:local="clr-namespace:ChatModule.ViewModels">
        <local:User Name="Person 1" />
        <local:User Name="Person 2" />
        <local:User Name="Person 3" />
        <local:User Name="Person 4" />
        <local:User Name="Person 5" />
        <local:User Name="Person 6" />
    </UserListViewModel.Users>
</UserListViewModel>

但是,当我摆脱后面的代码时,会遇到上面提到的相同错误(在构建中会消失),但还会显示一条错误消息
Cannot set properties on propety elements. Line 2 Position 6.

与其他错误不同,这不允许我进行构建。为什么是这样?我如何解决它?我仍然习惯于设计时的数据工作。

最佳答案

好吧,按照上面我的评论,摆弄它之后,我已经弄清楚了。显然,它的工作方式不同于 View xaml,因为我不需要两次指定xmlns:local即可访问属性的属性。我不知道为什么是这样。

这是我的代码现在的样子。

在我的主要xaml中,我有这个:

<Grid Name="LayoutRoot" d:DataContext="{d:DesignData Source=DesignTimeData/UserListViewData.xaml}">
    <ListView ItemsSource="{Binding Users}" ItemTemplate="{StaticResource UserTemplate}"  VerticalContentAlignment="Center" HorizontalContentAlignment="Stretch" Background="#505050" >
    </ListView>
</Grid>

在单独的xaml文件中,我有以下内容:
<local:UserListViewModel xmlns:local="clr-namespace:ChatModule.ViewModels">
    <local:UserListViewModel.Users >
        <local:User Name="Person 1" />
        <local:User Name="Person 2" />
        <local:User Name="Person 3" />
        <local:User Name="Person 4" />
        <local:User Name="Person 5" />
        <local:User Name="Person 6" />
    </local:UserListViewModel.Users>
</local:UserListViewModel>

我仍然在支持的xaml文件中获得“未找到属性'名称'...”,但是在 View 的主xaml文件中不再存在。我觉得这很优雅。并且以这种方式... View 仍然完全不属于实际的ViewModel类型...但是,当我像以前一样将它包含在 View xaml文件中时,即使它只是在设计时,它也必须知道该信息。

顺便说一下,这是在Visual Studio的xaml编辑器中。

这是一个有关错误的问题的示例,这使得它成为一件使事情变得正确的琐碎猜测游戏。除非我重新构建以清除错误,直到再次弄乱某些语法,否则没有智能和东西。它仍然可以用作设计时间数据,所以我不知道怎么回事。

关于wpf - WPF设计时间数据困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12241148/

相关文章:

c# - AccessibleObjectFromPoint 返回客户端对象而不是复选框

.net 4.5 中可绑定(bind)的 WPF Richtextbox

silverlight - 在我的 ViewModel 中使用 DataServiceCollection 时如何实现 "Blendability"

wpf - 是什么让 Visual Studio 的设计师参与设计时支持

wpf - 使用后台工作程序的MVVM nUnit类

wpf - 如何在 Expression Blend 中显示带有设计时源的图像

wpf - 样式按钮图像仅适用于最后一项

c# - wpf绑定(bind)问题

c# - 将 Unity 容器传递给 WPF Converter 类

wpf - MVVM 多命令处理程序