ListBox不是数据绑定

时间:2013-11-30 09:50:05

标签: c# .net wpf data-binding .net-4.5

我的ListBox控件正常工作,但要绑定的数据没有显示。

我的XAML:

<ListBox x:Name="listFileNames" SelectionMode="Single" Margin="10">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="60"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Image Margin="5" Source="{Binding Path=Image}" Stretch="Fill" Width="50" Height="50"></Image>
                <StackPanel Grid.Column="1" Margin="5">
                    <TextBlock Text="{Binding Path=FileName}" FontWeight="Bold"></TextBlock>
                    <TextBlock Text="{Binding Path=State}"></TextBlock>
                    <TextBlock Text="This text shows..."></TextBlock>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我的代码:

public struct StyleDocumentFile
{
    public string Image;
    public string FileName;
    public string State;
}

// ......

StyleDocumentFile sdf = new StyleDocumentFile()
{
    Image = "/Images/Loading.png",
    FileName = "abc",
    State = "Extracting Data...",
};

this.listFileNames.Items.Add(sdf);

2 个答案:

答案 0 :(得分:1)

将字段更改为Property。在此之后,一切正常。

public struct StyleDocumentFile
{
    public string Image { get; set; }
    public string FileName { get; set; }
    public string State { get; set; }
}

答案 1 :(得分:0)

您应该在ListBox定义中设置ItemsSource,如ItemsSource="{Binding Model.Items}"。此外,您必须在RaisePropertyChanged的模型属性中调用setter