2011-12-21 20 views
0

ListBox内にある2つの列のグリッドを実行しています。その後、私はDataBindを2つの列を縦に繰り返すことができます。グリッド内のグリッド付きのデータがありません

これまでのところ、以下のコードは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> 

私を助けてください。

+0

どのようにItemsSourceを設定していますか?編集:あなたのDataContext全体はItemssourceですか? –

+0

商品がまだありません。これが修正された後に設定します。 – darking050

+1

表示するアイテムがなくなるまで何も表示されません:) –

答えて

1

あなたの唯一の懸念は、あなたがアクションで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> 

注:

  • を私はのItemsSourceを取り出して、明示的にアイテムを供給しました。
  • アイテムは、テンプレート化されるようにUIElementから派生してはなりません。 (UIElementは単純に描画され、テンプレートは無視されます)
  • 文字列オブジェクトを指定できるようにSystem namespaceを追加しました。
  • 複数のリスト行が表示されるようにItemTemplateの高さを減らしました。

簡単ソリューション: は、リストボックスに名前を付け、バインディングを削除します。

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

次に()(コールのInitializeComponent後)のコードでこの行を使用します。

myLB.ItemsSource = new List<string> { "First", "Second", "Third" }; 
1

をデザイン時のitemssourceが必要な場合は、IsInDesignModeプロパティを次のように使用できます。

if (System.ComponentModel.DesignerProperties.IsInDesignTool) 
      { 
       myListBox.ItemsSource = GenerateMockItems(); 
      } 
      else 
      { 
       myListBox.ItemsSource = GetRealItems(); 
      } 
MVVMLightのviewmodelsで3210

、これはXAMLであなたのItemsSourceを設定しているように見えることから、あなたのDataContextは、あなたのクラスの中、あなたは

ような何かを行うことができ、同様に

if (IsInDesignMode) 
    { 

    } 

としてショートカット-EDです

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)); 
    } 
} 
関連する問題