【问题标题】:Add data to DataTemplatev - XAML/C#将数据添加到 DataTemplatev - XAML/C#
【发布时间】:2015-03-26 08:26:36
【问题描述】:

我有这个代码:

 <HubSection IsHeaderInteractive="True" x:Uid="Section3Header" Header="My Groups" Padding="40,40,40,32">
                <DataTemplate>
                    <ListView x:Name="List" Grid.Row="6" Margin="40,20,40,10" HorizontalAlignment="Stretch" Foreground="White">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Width="220" Height="60" Background="#FF729FD4">
                                    <Border Background="#66727272" Width="40" Height="40" Margin="10">
                                        <Image Source="/SampleImage.png" Height="32" Width="32" Stretch="UniformToFill"/>
                                    </Border>
                                    <StackPanel Orientation="Vertical" VerticalAlignment="Center">
                                        <TextBlock Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis"/>
                                    </StackPanel>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>

                    </ListView>
                </DataTemplate>
            </HubSection>

我的问题很基本: 我需要使用该代码创建一个列表视图,并且在每个列表视图项中都需要一个我通过 websrvice 获得的数据(类型是字符串)。

我的问题是如何将这些数据填充到 DataTemplate?

谢谢。

【问题讨论】:

    标签: c# xaml listview datatemplate


    【解决方案1】:

    我想你误会了什么。数据模板用于定义 ItemsControl(例如 ListView)的项目的视觉外观。

    在您的情况下,ListView 是另一个数据模板的一部分,这在某些情况下是有意义的(父子显示),但我认为这不是您想要的。 因此,在您的情况下,您只需删除外部数据模板,只需将项目列表分配给代码中的 ListView。

    如果您确实需要外部日期模板(我不熟悉 HubSection),那么 ListView 是数据模板的一部分,您需要将数据模板类型的属性绑定到 ListView 的 Items 集合。 比如:

    <ListView Items={Binding YourListProperty} ... >
    

    【讨论】:

      猜你喜欢
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多