【问题标题】:MVVM ItemTemplate with custom control带有自定义控件的 MVVM ItemTemplate
【发布时间】:2016-08-20 14:06:01
【问题描述】:

我正在关注本教程,了解如何使用 ListView ItemTemplate 显示人员列表:http://www.wpf-tutorial.com/listview-control/listview-data-binding-item-template/

我理解了一般概念,但我被困在一点上。

这是 ItemTemplate 示例:

    <Grid>
            <ListView Margin="10" Name="lvDataBinding" ItemsSource"={Binding MyPersonsList}">
                    <ListView.ItemTemplate>
                            <DataTemplate>
                                    <WrapPanel>
                                            <TextBlock Text="Name: " />
                                            <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                                            <TextBlock Text=", " />
                                            <TextBlock Text="Age: " />
                                            <TextBlock Text="{Binding Age}" FontWeight="Bold" />
                                            <TextBlock Text=" (" />
                                            <TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
                                            <TextBlock Text=")" />
                                    </WrapPanel>
                            </DataTemplate>
                    </ListView.ItemTemplate>
            </ListView>
    </Grid>

我无法理解的是如何用这样的自定义控件替换 TextBlocks:

    <Grid>
            <ListView Margin="10" Name="lvDataBinding" ItemsSource"={Binding MyPersonsList}">
                    <ListView.ItemTemplate>
                            <DataTemplate>
                                    <WrapPanel>
                                            <MyPersonDisplayer Person="{Binding ???}"/>
                                    </WrapPanel>
                            </DataTemplate>
                    </ListView.ItemTemplate>
            </ListView>
    </Grid>

如何告诉模板我想用源项设置MyPersonDisplayer控制属性Person


编辑:

我尝试仅添加 Person={Binding},但它显示此错误。

【问题讨论】:

  • 试试&lt;MyPersonDisplayer Person="{Binding}"/&gt;
  • @lok​​usking 我试过了,但是我有一个编译错误。查看我的编辑。
  • 删除文本块。只支持一个孩子
  • 它没有改变任何东西。另外,DataTemplate 中只有一个孩子:网格(包含多个孩子)
  • 能否提供MenuItemView的代码? XML & .cs

标签: c# xaml mvvm itemtemplate


【解决方案1】:

我认为在MenuItemView class 中实现MenuItemViewModel property 可能有问题。您是否将 MenuItemViewModel property 实现为 DependencyProperty?也许你可以参考这个post

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-28
    • 2011-12-02
    • 2023-03-17
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多