AgainMe AgainMe - 3 months ago 6
C# Question

Not able to see binded items to listbox

I've created a

ListBox
with this structure:

<ListBox VerticalAlignment="Stretch"
Background="AliceBlue"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ItemsSource="{Binding EventInfo}">


how you can see I binded the
EventInfo
property that I valorize behind code. This property have the
OnPropertyChange();
implementation as my other properties, and the value setted is got correctly. Anyway, I'm not able to display the binded source:

<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=League}" />
<TextBlock Text="test" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>


now the property
League
value isn't displayed also the value test. I really don't understand why. The
League
property exist, and also I've no error in xaml.

What I did wrong?

UPDATE:

public Models.EventInfo EventInfo
{
get { return _eventInfo; }
set
{
_eventInfo = value;
OnPropertyChanged();
}
}


and in the
Model


public class EventInfo
{
public string League { get; set; }
public string Date { get; set; }
public string GameWeek { get; set; }
public string GameStart { get; set; }
public string FirstTime { get; set; }
public string SecondTime { get; set; }
public string Stadium { get; set; }
public List<MatchArbiter> Arbiter { get; set; }
}

Answer

Try this. You need to populate ItemsSource with a collection, not a single item. Instead of your existing EventInfo property, you need a collection property. I'm going to rename it to EventInfoItems to keep confusion to a minimum.

private ObservableCollection<Models.EventInfo> _eventInfoItems = 
    new ObservableCollection<Models.EventInfo>();

public ObservableCollection<Models.EventInfo> EventInfoItems
{
    get { _eventInfoItems; }
    set
    {
        _eventInfoItems = value;
        OnPropertyChanged();
    }
}

Now, somewhere, you're going to have to add some items to that collection if you want anything to appear in the list. You could create a few test items in your viewmodel constructor, just for the time being. Like this:

    EventInfoItems.Add(new EventInfo { League = "NBA" });
    EventInfoItems.Add(new EventInfo { League = "Premier League" });
    EventInfoItems.Add(new EventInfo { League = "Serie A" });

XAML

<ListBox 
    VerticalAlignment="Stretch" 
    Background="AliceBlue"
    ScrollViewer.CanContentScroll="True" 
    ScrollViewer.VerticalScrollBarVisibility="Visible"
    ItemsSource="{Binding EventInfoItems}"
    >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
               <TextBlock Text="{Binding Path=League}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>

Update

Turns out OP may have only one item. If that's the case, a ListBox is unnecessary. A ContentControl is the right control when you've got only one item and you want to display it with a DataTemplate. This XAML will use the original version of the EventInfo property:

public Models.EventInfo EventInfo
{
    get { return _eventInfo; }
    set
    {
        _eventInfo = value;
        OnPropertyChanged();
    }
}

XAML:

<ContentControl 
    VerticalAlignment="Stretch" 
    Background="AliceBlue"
    ScrollViewer.CanContentScroll="True" 
    ScrollViewer.VerticalScrollBarVisibility="Visible"
    Content="{Binding EventInfo}"
    >
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
               <TextBlock Text="{Binding Path=League}" />
            </StackPanel>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>
Comments