【问题标题】:binding images from xml to listbox in windows phone application在 Windows Phone 应用程序中将图像从 xml 绑定到列表框
【发布时间】:2013-01-03 20:16:16
【问题描述】:

我的xml文件是这样的

<people><person> <firstname>venakt</firstname> <lastname>es</lastname> <age>27</age> <Image>http://www.livetut.com/wp-content/uploads/2012/06/Logo1.png</Image> </person></people>

我的 XAML 文件看起来像

<ListBox.ItemTemplate>
 <DataTemplate>
  <StackPanel>
    <Image Source="{Binding ImageSource}" Height="120" Width="120" HorizontalAlignment="Center" VerticalAlignment="Center" Tap="textBlock1_tap" />
    <TextBlock Text="{Binding UserName}" Style="{StaticResource PhoneTextSubtleStyle}" Width="100" TextAlignment="Center"/>
  </StackPanel>
 </DataTemplate>
</ListBox.ItemTemplate>

在我的 xaml.cs 文件中我添加了

public string Image { get { return Image; } set { Image = value; } }



XDocument loadedData = XDocument.Load("People.xml");
    var data = from query in loadedData.Descendants("person")
                  select new Person
                  {
                      FirstName = (string)query.Element("firstname"),
                      LastName = (string)query.Element("lastname"),
                      Image= query.Element("Image").Attribute("url").Value

                  };
    listBox.ItemsSource = data;

你能帮我怎么绑定图片吗

【问题讨论】:

    标签: windows-phone-7 linq-to-xml


    【解决方案1】:

    只需将名称 ImageSource 更改为 Image (Image 是您的 Person 类的属性名称 .. 所以)

    <ListBox.ItemTemplate>
     <DataTemplate>
      <StackPanel>
        <Image Source="{Binding Image}" Height="120" Width="120" HorizontalAlignment="Center" VerticalAlignment="Center" Tap="textBlock1_tap" />
        <TextBlock Text="{Binding UserName}" Style="{StaticResource PhoneTextSubtleStyle}" Width="100" TextAlignment="Center"/>
      </StackPanel>
     </DataTemplate>
    </ListBox.ItemTemplate>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      相关资源
      最近更新 更多