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();

    }
}
4

3 回答 3

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}"

于 2013-03-07T19:14:40.870 回答
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>
于 2013-03-07T19:13:08.273 回答
0

我会尝试使用 ViewModel ...

好文章

另一篇好文章

这两篇文章重点介绍了完全绑定的好处,并进入了 NotifyPropertyChanged 和命令。值得一读。

于 2013-03-07T20:56:40.840 回答