rhughes
rhughes

Reputation: 9583

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

Upvotes: 0

Views: 67

Answers (2)

Afshar
Afshar

Reputation: 11533

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

Upvotes: 0

ebattulga
ebattulga

Reputation: 11001

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

Upvotes: 1

Related Questions