Xaml 列表框中没有包含网格的数据

Xaml 列表框中没有包含网格的数据,xaml,windows-phone-7,Xaml,Windows Phone 7,我正在做一个列表框中两列的网格。之后,我可以将两列数据绑定到垂直重复的列 到目前为止,下面的代码在WP7模拟器上没有显示任何内容 <ListBox Background="Yellow" ItemsSource="{Binding}" Height="100" Margin="0,0,8,0"> <ListBox.ItemTemplate> <DataTemplate> <Grid Height="100"> <Grid.ColumnDefi

我正在做一个列表框中两列的网格。之后,我可以将两列数据绑定到垂直重复的列

到目前为止,下面的代码在WP7模拟器上没有显示任何内容

<ListBox Background="Yellow" ItemsSource="{Binding}" Height="100" Margin="0,0,8,0">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="100">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<TextBlock Text="Channels" HorizontalAlignment="Stretch" Foreground="Black" Grid.Column="0" />
<TextBlock Text="Antenna" HorizontalAlignment="Stretch" Foreground="Black" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>


请帮助我。

如果您唯一关心的是看到ItemTemplate正在运行,您可以按如下方式提供显式非UI项:

<ListBox Background="Yellow" Height="100" Margin="0,0,8,0" xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Height="30">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition Width="200" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="Channels" HorizontalAlignment="Stretch" Foreground="Black" Grid.Column="0" />
                <TextBlock Text="Antenna" HorizontalAlignment="Stretch" Foreground="Black" Grid.Column="1"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <sys:String>1111111</sys:String>
    <sys:String>2222222</sys:String>
    <sys:String>3333333</sys:String>
</ListBox>

1111111
2222222
3333333
注:

  • 我删除了ItemsSource并明确提供了项目
  • 项不能派生自UIElement,以便对其进行模板化。(只绘制UIElements,而忽略模板。)
  • 我添加了系统名称空间,以便可以指定字符串对象
  • 我降低了ItemTemplate的高度,以便可以看到多个列表行
更简单的解决方案: 为列表框指定名称并删除绑定:

<ListBox x:Name="myLB" Background="Yellow" Height="100" Margin="0,0,8,0"> 

然后在代码中使用此行(在调用InitializeComponent()之后):

myLB.ItemsSource=新列表{“第一”、“第二”、“第三”};

如果需要设计时项目资源,可以使用IsInDesignMode属性,如下所示:

 if (System.ComponentModel.DesignerProperties.IsInDesignTool)
            {
                myListBox.ItemsSource = GenerateMockItems();
            }
            else
            {
                myListBox.ItemsSource = GetRealItems();
            }
在MVVMLight ViewModels中,此快捷方式被设置为

    if (IsInDesignMode)
    {

    }
类似地,因为看起来您是在xaml中设置ItemsSource,在作为DataContext的类中,您可以执行以下操作

public class MyViewModel
{
  public MyViewModel()
  {
     if (System.ComponentModel.DesignerProperties.IsInDesignTool)
                {
                    Items = GenerateMockItems();
                    EditTime = GenerateRandomFutureDate();
                }
                else
                {
                   //whatever you expect should happen in runtime
                }
  }

  //what list is binding to
  public ObservableCollection<Item> Items { get; set; }

   //other properties.. for example
   public bool HasItems { get { return Items != null && Items.Count > 0; } }

   public DateTime EditDate { get; set; }

   private ObservableCollection<Item> GenerateMockItems()
  {
      var collection = new ObservableCollection<Item>();
      for (int i = 0; i < 10; i++)
      {
          collection.Add(new Item() { Name="sdfsdfs" , Channel=i });
      }
      return collection;
  }
  private DateTime GenerateRandomFutureDate()
  {
      return DateTime.Now.AddSeconds(new Random().Next(0,50000));
  }
}
公共类MyViewModel
{
公共MyViewModel()
{
if(System.ComponentModel.DesignerProperties.IsInDesignTool)
{
Items=GenerateMockItems();
EditTime=GeneratorDomainFutureDate();
}
其他的
{
//无论您期望什么都应该在运行时发生
}
}
//该列表绑定到什么
公共ObservableCollection项{get;set;}
//其他属性..例如
public bool HasItems{get{return Items!=null&&Items.Count>0;}
公共日期时间编辑日期{get;set;}
私有ObservableCollection GenerateMockItems()
{
var collection=新的ObservableCollection();
对于(int i=0;i<10;i++)
{
添加(新项(){Name=“sdfsdfs”,Channel=i});
}
回收;
}
专用日期时间生成器DomFutureDate()
{
return DateTime.Now.AddSeconds(new Random().Next(050000));
}
}

您如何设置项目资源?编辑:您的整个DataContext就是Itemssource?还没有itemsource,我会在修复后进行设置。在有项目要显示之前,不会显示任何内容:)在我准备好Itemssource之前,我现在如何添加虚拟文本?
public class MyViewModel
{
  public MyViewModel()
  {
     if (System.ComponentModel.DesignerProperties.IsInDesignTool)
                {
                    Items = GenerateMockItems();
                    EditTime = GenerateRandomFutureDate();
                }
                else
                {
                   //whatever you expect should happen in runtime
                }
  }

  //what list is binding to
  public ObservableCollection<Item> Items { get; set; }

   //other properties.. for example
   public bool HasItems { get { return Items != null && Items.Count > 0; } }

   public DateTime EditDate { get; set; }

   private ObservableCollection<Item> GenerateMockItems()
  {
      var collection = new ObservableCollection<Item>();
      for (int i = 0; i < 10; i++)
      {
          collection.Add(new Item() { Name="sdfsdfs" , Channel=i });
      }
      return collection;
  }
  private DateTime GenerateRandomFutureDate()
  {
      return DateTime.Now.AddSeconds(new Random().Next(0,50000));
  }
}