2013-03-07 182 views
0

我有以下代碼。它構建並運行,但不填充列表框。有人能發現錯誤嗎?數據綁定到列表框項目模板不起作用

<Grid> 
     <ListBox ItemsSource="{Binding Path=questions}" Height="401" HorizontalAlignment="Left" Name="results" VerticalAlignment="Top" Width="260" Margin="0,20,0,0"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Vertical"> 
          <StackPanel Orientation="Horizontal"> 
           <StackPanel Orientation="Vertical"> 
            <TextBlock Text="{Binding Path=question.votes}" FontSize="15" Padding="5" Background="White" Foreground="Black"/> 
           <TextBlock Text="{Binding Path=question.answers}" FontSize="15" Padding="5" Background="White" Foreground="Black"/> 
           </StackPanel> 
           <StackPanel Orientation="Vertical" Height="Auto" Width="249"> 
           <TextBlock Text="{Binding Path=question.title}" FontWeight="Bold" Background="#FF92F2CD" Height="22" Width="229" Foreground="Black"/> 
           <TextBlock Text="{Binding Path=question.body}" TextWrapping="Wrap" Height="43" Width="231" Background="#FFEFEFEF" Foreground="Black"/> 
           </StackPanel> 
          </StackPanel> 
          <StackPanel> 
          <TextBlock Text="{Binding Path=question.tags}" Foreground="#FFFF9C00" Background="#FF4E3D3D" FontWeight="Bold" TextAlignment="Center"/> 
          </StackPanel> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate>  
     </ListBox> 
     <Button Content="Refresh" Height="22" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="54" /> 
    </Grid> 




public class question 
{ 
    public string votes  { get; set; } 
    public string answers { get; set; } 
    public string title  { get; set; } 
    public string body  { get; set; } 
    public string tags  { get; set; } 

} 

public partial class MainWindow : Window 
{ 

    ObservableCollection<question> questions = new ObservableCollection<question>(); 

    public MainWindow() 
    { 
     questions.Add(new question 
     { 
      votes = "2", 
      answers = "3", 
     title = "This is a sample title", 
     body = "This is a sample body text. It should wrap and not look like shit when presented.", 
     tags = "C#,WPF,XML,JediStyle" 

    }); 
    this.DataContext = this; 

    InitializeComponent(); 

    } 
} 
+0

是問題和問題兩個不同的屬性? – ethicallogics 2013-03-07 19:14:09

回答

2

約束力的領域,但對性能不起作用。

ObservableCollection<question> questions = new ObservableCollection<question>(); 

ObservableCollection<question> MyQuestions 
{ 
    get { return questions; } 
} 

而在XAML

ItemsSource="{Binding Path=MyQuestions}" 

你也不必指定question爲特定列表項中的每一個綁定路徑的一部分:

Text="{Binding Path=question.tags}"應該Text="{Binding Path=tags}"或者更簡單:Text="{Binding tags}"

0
 <ListBox ItemsSource="{Binding Path=questions}" Height="401" HorizontalAlignment="Left" Name="results" VerticalAlignment="Top" Width="260" Margin="0,20,0,0"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Vertical"> 
         <StackPanel Orientation="Horizontal"> 
          <StackPanel Orientation="Vertical"> 
           <TextBlock Text="{Binding Path=votes}" FontSize="15" Padding="5" Background="White" Foreground="Black"/> 
          <TextBlock Text="{Binding Path=answers}" FontSize="15" Padding="5" Background="White" Foreground="Black"/> 
          </StackPanel> 
          <StackPanel Orientation="Vertical" Height="Auto" Width="249"> 
          <TextBlock Text="{Binding Path=title}" FontWeight="Bold" Background="#FF92F2CD" Height="22" Width="229" Foreground="Black"/> 
          <TextBlock Text="{Binding Path=body}" TextWrapping="Wrap" Height="43" Width="231" Background="#FFEFEFEF" Foreground="Black"/> 
          </StackPanel> 
         </StackPanel> 
         <StackPanel> 
         <TextBlock Text="{Binding Path=tags}" //am not sure from where this tags coming 
Foreground="#FFFF9C00" Background="#FF4E3D3D" FontWeight="Bold" TextAlignment="Center"/> 
         </StackPanel> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate>  
    </ListBox> 
0

我會嘗試使用ViewModel ...

Good Article

Another Good Article

的兩篇文章強調完全結合的好處,並進入NotifyPropertyChanged和命令。值得一讀。