【发布时间】:2014-10-15 04:14:09
【问题描述】:
我正在尝试将项目添加到列表视图中,其中每个项目都有两个文本块,但由于某些原因项目没有显示。这是我的 XAML 代码。
<Window x:Class="testapp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="524" Width="856">
<Grid Background="Black">
<!--<Image Height="44" HorizontalAlignment="Left" Margin="284,0,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="229" Source="/testapp;component/Images/Picture1.png" />-->
<Grid Height="431" HorizontalAlignment="Left" Margin="0,55,0,0" Name="grid1" VerticalAlignment="Top" Width="266" Background="Black" Opacity="0">
<ListView Height="430" HorizontalAlignment="Left" Margin="3,1,0,0" x:Name="listView1" VerticalAlignment="Top" Width="260" >
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Name}" Foreground="Green" />
<TextBlock Text="{Binding Source}" Foreground="Green" />
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
<!-- <ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>-->
</ListView>
</Grid>
</Grid>
</Window>
这是我的 C# 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace testapp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<MyItems> items = new List<MyItems>();
items.Add(new MyItems() { Name = "House Party", Source = " DLNA" });
items.Add(new MyItems() { Name = "Outdoor Party", Source = " DLNA" });
listView1.ItemsSource = items;
}
}
public class MyItems
{
public String Name { get; set; }
public String Source { get; set; }
}
}
即使我将文本块绑定到名称和来源,它仍然没有显示。有人可以帮助我吗?提前致谢。
【问题讨论】: