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 に答える