1

我很有趣,我可以将一些控件(图像,2-3 个文本框)分组到一个元素中,然后将这些元素的组推送到列表框中吗?我正在尝试将俄罗斯社交网络 Vkontakte 的新闻阅读器变成 Windwos Phone 7。

每条新闻都有图像、文本和其他一些元数据。所以我想将所有这些信息分组到一个控件中并在列表框中使用它。我试图将一个网格(它有一个图像和两个文本框)推送到列表框中,但它会抛出 XamlParseException。另外,我需要从代码中获取这些文本框和图像的内容。在网格中我可以使用

<Grid.Resources>
    <src:Customers x:Key="customers"/>
</Grid.Resources>
4

2 回答 2

1

这是您需要的:

  • 模型的集合(ObservableCollection<T>推荐)(News在您的情况下)。
  • 一种ListBox
  • 一种DataTemplate

例子:

XAML:

<ListBox Name="ListBox1">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                ...
                <Image Source="{Binding ImagePropertyInModel}" ... />
                <TextBlock Text="{Binding TextPropertyInModel}" ... />
                ...
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

后面的代码:

ListBox1.ItemsSource = <collection of models>;

您可以使用( ) 或您可能已经拥有的 a 来代替<Grid>in 。<DataTemplate>Custom ControlTemplated ControlUser Control

于 2012-11-28T18:56:43.790 回答
0

您可以使用要使用的控件创建 UserControl。使用您的 ListBox,您可以执行以下操作:

<ListBox ItemsSource="{Binding Path=YourItemsSource}">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <my:MyUserControl DataContext="{Binding}"/>
      </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

作为 ListBox 的 ItemsSource,您可以使用新闻的 ObservableCollection,并通过DataContext="{Binding}"您可以绑定到 UserControl 中的新闻属性,例如:

<UserControl...>
    <Image Source="{Binding Path=photoAttachment}"/>
</UserControl>
于 2012-11-28T18:56:01.667 回答