Question

I gave up on my code bugs so I decide to write here. I really hope to get a solution. What I want with the listbox is: when i click the button, it will retrieve data from database then load it into the listbox. It worked fine. But when I add wpf style, it started problem because I want to add image into each item next to text - image (right side) and text (left side). The result in the listbox is blank but actually it seems there is a data list - please look at the picture. I may have done something wrong in my code or wpf. I am not sure what is problem.... I would appreciate if you can have a look at my code. Your given code would be much helpful. Thanks alot!

WPF:

    <ListBox Name="lstDinner" DisplayMemberPath="Name" Margin="513,85,608,445" Style="{DynamicResource ResourceKey=styleListBox}"/>

WPF STYLE:

 <DataTemplate x:Key="templateListBoxItem">
    <Grid Margin="5">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Border Grid.Column="0"
                Grid.Row="0"
                Grid.RowSpan="2"
                Margin="0,0,10,0">
            <Image Source="{Binding Path=Image}"
                   Stretch="Fill"
                   Height="40"
                   Width="40"></Image>
        </Border>
        <TextBlock Text="{Binding Path=Name}"
                   FontWeight="Bold"
                   Grid.Column="1"
                   Grid.Row="0"></TextBlock>
        <TextBlock Text="{Binding Path=About}"
                   Grid.Column="1"
                   Grid.Row="1"></TextBlock>
    </Grid>
</DataTemplate>

<Style x:Key="styleListBox" TargetType="{x:Type ListBox}">
    <Setter Property="ItemTemplate" Value="{StaticResource ResourceKey=templateListBoxItem}"></Setter>
</Style>

C#:

   private void Button_Click(object sender, RoutedEventArgs e)
    {
        _dinnerExtractor = new DinnerExtractor();
        const int oneDay = 1;

        _databaseDinnerList = new ObservableCollection<FoodInformation>(_dinnerExtractor.GetDinnerDays(oneDay));

        if (_databaseDinnerList != null)
        {
            foreach (var list in _databaseDinnerList)
            {

                lstDinner.Items.Add(new FoodInformation { Dinner = list.Dinner, DinnerImage = list.DinnerImage});
            }

            //lstDinner.ItemsSource = _databaseDinnerList;
        }
    }

enter image description here

Was it helpful?

Solution

FoodInformation class does not contains properties: Image and Name (you are trying binding to these properties in DataTemplate).

From code-behind we can create definition of FoodInformation class with properties Dinner and DinnerImage:

class FoodInformation
{
    public string Dinner { get; set; }
    public ImageSource DinnerImage { get; set; }
}

So you should binding to properties Dinner and DinnerImage, not to Image and Name.

If you change in DataTemplate appropriate properties names everything will be ok.

<DataTemplate x:Key="templateListBoxItem">
    <Grid Margin="5">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Border Grid.Column="0"
                Grid.Row="0"
                Grid.RowSpan="2"
                Margin="0,0,10,0">
            <Image Source="{Binding Path=DinnerImage }"
                   Stretch="Fill"
                   Height="40"
                   Width="40"></Image>
        </Border>
        <TextBlock Text="{Binding Path=Dinner }"
                   FontWeight="Bold"
                   Grid.Column="1"
                   Grid.Row="0"></TextBlock>
    </Grid>
</DataTemplate>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top