列表框项模板的数据绑定不起作用

本文关键字:数据绑定 不起作用 列表 | 更新日期: 2023-09-27 18:02:44

我有以下代码。它构建并运行,但不填充列表框。有人能发现这个错误吗?

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

列表框项模板的数据绑定不起作用

绑定不能作用于字段,只能作用于属性。

ObservableCollection<question> questions = new ObservableCollection<question>();
ObservableCollection<question> MyQuestions
{
    get { return questions; }
}

和XAML

ItemsSource="{Binding Path=MyQuestions}"

您也不必为特定列表项中的每个绑定指定question作为path的一部分:

Text="{Binding Path=question.tags}"应该是Text="{Binding Path=tags}"或者更简单:Text="{Binding tags}"

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

我会尝试使用ViewModel…

篇好文章

又一篇好文章

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