绑定数据 - inotifypropertychanged不起作用

时间:2014-09-14 10:50:38

标签: c# wpf inotifypropertychanged

我有一个listBox1,其中数据从列表中绑定。然后我想当我从listBox2中的listBox1中选择任何项目时将绑定来自另一个列表的数据。

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

这是第一次工作,但是对于两次列表,boxBox2不会显示新数据。

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

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

2 个答案:

答案 0 :(得分:-1)

RaisePropertyChanged("teamList");

错误你的名字被命名为'teamsList',带有S,

更改为:

RaisePropertyChanged("teamsList");

它是您绑定的公共属性并通知其更改

编辑:

也会更改你的绑定:

ItemsSource="{Binding teamList}"

ItemsSource="{Binding teamsList}" 

编辑2:

listBox2.DataContext = xxx

不是itemsource = xxx

答案 1 :(得分:-3)

使用该行(listBox1_SelectionChanged

listBox2.ItemsSource = lists.h2hList;

您正在有效地从listBox2的ItemsSource属性中删除绑定。

相反,您应该只更新h2hList课程中的Lists媒体资源(可能发生在getH2hResults中),并从您的代码中移除上一行。

但请注意,清除并重新填写该列表是不够的。您需要设置h2hList属性才能获得引发的属性更改通知:

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

如果要保留列表并只更改其元素,则需要使用ObservableCollection<H2H>而不是List<H2H>作为集合类型。无论如何,这将是更好的方法,因为您不必关心何时将元素添加到新创建的集合中。