2011-12-21 5 views
0

ListBox 내에있는 두 개의 열로 이루어진 Grid를 수행하고 있습니다. 그 후에는 DataBind를 사용하여 두 열을 세로로 반복 할 수 있습니다.Grid가있는 ListBox가있는 데이터가 없습니다

지금까지 아래 코드는 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을 볼 것입니다 경우 다음과 같이

+0

어떻게 ItemsSource를 설정하고 있습니까? 편집 : 전체 DataContext는 Itemssource입니까? –

+0

아직 항목 소스가 없으므로 수정 한 후에 설정하겠습니다. – darking050

+1

표시 할 항목이있을 때까지 아무 것도 표시되지 않습니다. –

답변

1

, 당신은 명시 적으로 비 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 네임 스페이스를 추가했습니다.
  • 하나 이상의 목록 행을 볼 수 있도록 ItemTemplate 높이를 줄였습니다.

쉬운 솔루션 : 이 목록 상자에 이름을 지정하고 바인딩을 제거 :() (통화의 InitializeComponent 후)

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

그런 다음 코드에서이 줄을 사용하십시오 :

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

디자인 타임 아이템 소스를 원할 경우 IsInDesignMode 속성을 다음과 같이 사용할 수 있습니다.

if (System.ComponentModel.DesignerProperties.IsInDesignTool) 
      { 
       myListBox.ItemsSource = GenerateMockItems(); 
      } 
      else 
      { 
       myListBox.ItemsSource = GetRealItems(); 
      } 
당신이 XAML에서 ItemsSource를 설정하는 것처럼, 당신은

같은 것을 할 수있는 당신의 DataContext입니다 클래스 내부 보인다 이후 MVVMLight ViewModels에서

(3210)는,이, 마찬가지로

if (IsInDesignMode) 
    { 

    } 

로 바로 가기 - 에드

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)); 
    } 
} 
관련 문제