-1

I have a listBox1 in which data are binding from the list. Then I want to when I select any item from listBox1 in listBox2 will binding data from another list.

    private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Teams teams = (Teams)listBox1.SelectedItems[0];
        getH2hResults("//td[@class='hell']", teams.Team1, teams.Team2);                      // add elements to list
        getH2hResults("//td[@class='dunkel']", teams.Team1, teams.Team2);                 // and here also
        listBox2.ItemsSource = lists.h2hList;
    }

On the first time this work, but for the twice time listBox2 doesn't displays new data.

     public class Lists : BindableBase
{
    public Lists()
    {
        _teamsList = new List<Teams>();
        _h2hList = new List<H2H>();
    }
    private List<Teams> _teamsList;

    public List<Teams> teamsList
    {
        get
        {
            return _teamsList;
        }
        set
        {
            if (value != _teamsList)
            {
                _teamsList = value;
                RaisePropertyChanged("teamsList");
            }
        }
    }

    private List<H2H> _h2hList;

    public List<H2H> h2hList
    {
        get
        {
            return _h2hList;
        }
        set
        {
            if (value != _h2hList)
            {
                _h2hList = value;
                RaisePropertyChanged("h2hList");
            }
        }
    }
}

And XAML

       <ListBox Name="listBox1" Width="300" Height="300"
             VerticalAlignment="Top"
             HorizontalAlignment="Left"
             ItemsSource="{Binding teamsList}" SelectionChanged="listBox1_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Foreground="#FF4273CD" Text="{Binding Team1, Mode=TwoWay}"></TextBlock>
                    <TextBlock Text=" vs " FontWeight="Bold"></TextBlock>
                    <TextBlock Foreground="#FF4273CD" Text="{Binding Team2, Mode=TwoWay}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <ListBox Name="listBox2" Grid.Column="1" Width="300" Height="300"
             VerticalAlignment="Top"
             HorizontalAlignment="Left"
             ItemsSource="{Binding h2hList}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding date, Mode=TwoWay}"></TextBlock>
                    <TextBlock Text="{Binding result, Mode=TwoWay}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
user140503
  • 127
  • 1
  • 8
  • There are widely accepted [capitalization conventions](http://msdn.microsoft.com/en-us/library/ms229043.aspx) for public identifiers (like property names) in C#. They should use `PascalCasing` instead of `camelCasing`. Another note, a TwoWay binding on the Text property of a TextBlock doesn't make any sense. – Clemens Sep 14 '14 at 11:11
  • 1
    May I ask why you are not using for instance `ObservableCollection` and just `Clear`ing and repopulating your Lists? Then you wouldn't even have to trigger any `INotifyPropertyChanged` (`ObservableCollection` would do it for you) – default Sep 14 '14 at 11:13
  • @Default True, but keep in mind that adding new elements to an ObservableCollection generates a collection change notification for each new element, plus one for clearing the collection, whereas replacing the list generates only a single property change notification. Hence the latter is much more efficient in case the lists elements are all replaced. – Clemens Sep 14 '14 at 11:23
  • 2
    @Clemens That is why I was asking OP for clarification of why he's not using OC. for relatively small lists, that overhead feels very irrelevant and in my opinion OC is much more easy to work with when you need to update your UI. – default Sep 14 '14 at 11:29

2 Answers2

-1
RaisePropertyChanged("teamList");

is Wrong your propery is named 'teamsList' with an S,

change to:

RaisePropertyChanged("teamsList");

It is the public property you bind to and notify changes of,

edit:

also change your binding:

ItemsSource="{Binding teamList}"

to

ItemsSource="{Binding teamsList}" 

Edit 2:

listBox2.DataContext = xxx

Not itemsource = xxx

Mark Homer
  • 960
  • 6
  • 15
  • yes, this was wrong, but this not fix the problem. listBox2 still does not update. – user140503 Sep 14 '14 at 11:05
  • -1 for `Edit 2: listBox2.DataContext = ...`. That is plain nonsense. It is not necessary and poor design to reset a control's DataContext in order to refresh a bound property. The change notification raised by the `h2hList` property is sufficient. Remove that edit and I'll be happy to remove my downvote (and you might consider to remove yours on my answer). – Clemens Sep 15 '14 at 10:06
  • @Clemens lol really it is not nonsense and is not wrong, the method he was using it works perfectly well, the majority of the answer covers issues you didn't even notice with your cut and paste response – Mark Homer Sep 15 '14 at 10:19
  • Apparently you haven't read my answer. I was telling him to **remove** the line where he sets the ItemsSource property, because doing so effectivly removes the binding from that property. – Clemens Sep 15 '14 at 10:22
-3

With the line (in listBox1_SelectionChanged)

listBox2.ItemsSource = lists.h2hList;

you are effectively removing the binding from the ItemsSource property of listBox2.

Instead, you should only update the h2hList property in your Lists class (which presumably happens in getH2hResults) and remove the above line from your code.

Note however that it is not sufficient to clear and re-fill that list. You need to set the h2hList property in order to get a property change notification raised:

var newList = new List<H2H>();
// fill newList before assigning to h2hList property
lists.h2hList = newList;

If you want to keep the list and just change its elements, you would need to use ObservableCollection<H2H> instead of List<H2H> as collection type. This would be the better approach anyway, as you would not have to care for when exactly you add elements to a newly created collection.

Clemens
  • 123,504
  • 12
  • 155
  • 268