简体   繁体   中英

ListBox not databinding

My ListBox control is working fine, except that the data to be bound is not displaying.

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

My code:

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

Change fields to Property. After this, all works fine.

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

You should set ItemsSource in ListBox definition like ItemsSource="{Binding Model.Items}" . In addition you must call RaisePropertyChanged in setter of model properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM