Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定数据-inotifypropertychanged不起作用_C#_Wpf_Inotifypropertychanged - Fatal编程技术网

C# 绑定数据-inotifypropertychanged不起作用

C# 绑定数据-inotifypropertychanged不起作用,c#,wpf,inotifypropertychanged,C#,Wpf,Inotifypropertychanged,我有一个listBox1,其中数据绑定自列表。然后我想知道,当我从listBox2中的listBox1中选择任何项目时,将绑定另一个列表中的数据 private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { Teams teams = (Teams)listBox1.SelectedItems[0]; getH2hResults("//td[

我有一个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;
    }
这是第一次工作,但两次listBox2不显示新数据

     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");
            }
        }
    }
}
公共类列表:BindableBase
{
公开名单()
{
_teamsList=新列表();
_h2hList=新列表();
}
私人名单(团队名单),;
公共列表团队列表
{
得到
{
返回团队列表;
}
设置
{
如果(值!=\u团队列表)
{
_团队列表=价值;
RaisePropertyChanged(“团队列表”);
}
}
}
私人名单;
公开名单
{
得到
{
返回列表;
}
设置
{
如果(值!=\u h2hList)
{
_h2hList=值;
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>

错误吗?您的财产被命名为带有S的“teamsList”

改为:

RaisePropertyChanged("teamsList");
它是您绑定并通知变更的公共财产

编辑:

还要更改您的绑定:

ItemsSource="{Binding teamList}"

编辑2:

listBox2.DataContext=xxx

Not itemsource=xxx

带有该行(在
列表框1中\u选择已更改

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

相反,您应该只更新
列表
类中的
h2hList
属性(这可能发生在
getH2hResults
中),并从代码中删除上述行

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

var newList = new List<H2H>();
// fill newList before assigning to h2hList property
lists.h2hList = newList;
var newList=newList();
//在指定给h2hList属性之前填写新列表
lists.h2hList=newList;


如果要保留列表并仅更改其元素,则需要使用
observetecollection
而不是
list
作为集合类型。无论如何,这是一种更好的方法,因为您不必关心何时向新创建的集合添加元素。

是的,这是错误的,但这并不能解决问题。listBox2仍然没有更新。
编辑2的-1:listBox2.DataContext=…
。那完全是胡说八道。为刷新绑定属性而重置控件的DataContext是不必要的,而且设计很差。
h2hList
属性引发的更改通知已足够。删除那个编辑,我很乐意删除我的投票(你可以考虑删除你的答案)。“克莱门斯?洛尔,这真的不是胡说八道,没错,他使用的方法效果很好,大部分答案都涵盖了你甚至没有注意到的问题,包括你的剪切和粘贴响应。家长们,你还没有读过我的答案。我告诉他删除设置ItemsSource属性的行,因为这样做可以有效地删除该属性的绑定。他们应该使用
PascalCasing
而不是
camelCasing
。另一个注意事项是,对TextBlock的Text属性进行双向绑定没有任何意义。我可以问一下,您为什么不使用例如
ObservableCollection
,而只是
Clear
对列表进行填充和重新填充?这样,您甚至不必触发任何
INotifyPropertyChanged
observateCollection
将为您执行此操作)@Default True,但请记住,向observateCollection添加新元素会为每个新元素生成一个集合更改通知,外加一个用于清除集合的通知,而替换列表只会生成一个属性更改通知。因此,如果列表元素都被替换,后者的效率会更高。@Clemens,这就是为什么我要求OP澄清为什么他不使用OC。对于相对较小的列表,这种开销感觉非常不相关,在我看来,当您需要更新UI时,OC更容易处理。我如何设置H2hList以获取属性更改通知?如上所述,通过设置
list.H2hList
属性。请参阅我的编辑。如果您也将
列表
替换为
可观察收集
,效果会更好。
ItemsSource="{Binding teamsList}" 
listBox2.ItemsSource = lists.h2hList;
var newList = new List<H2H>();
// fill newList before assigning to h2hList property
lists.h2hList = newList;