WPF ListBox:绑定到 ObservableCollection

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14191567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:55:57  来源:igfitidea点击:

WPF ListBox: Binding to an ObservableCollection

wpfbindinglistboxobservablecollection

提问by dotNET

I'm binding my ListBox to an ObservableCollection at runtime. Upon clicking a button, one of the items in my collection gets modified, but the corresponding ListBox item doesn't update itself accordingly. I have gone thru several similar SO articles and other help material and it seems like I'm doing everything they've asked for, but no luck. Everything seems to load and bind correctly, but when I change "IsLoading" property of an item in my collection, the Visibility of the Grid (see DataTemplate below) which is bound to IsLoading property doesn't change.

我在运行时将我的 ListBox 绑定到 ObservableCollection。单击按钮后,我的集合中的一项被修改,但相应的 ListBox 项不会相应地更新自身。我已经浏览了几篇类似的 SO 文章和其他帮助材料,似乎我正在做他们要求的一切,但没有运气。一切似乎都正确加载和绑定,但是当我更改集合中某个项目的“IsLoading”属性时,绑定到 IsLoading 属性的网格的可见性(请参阅下面的 DataTemplate)不会更改。

Following is my ListBox XAML:

以下是我的 ListBox XAML:

        <ListBox Name="lstItems">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Name="ListBoxGrid">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="120"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="100"/>
                        </Grid.ColumnDefinitions>
                        <CheckBox Grid.Column="0" IsChecked="{Binding IsSelected}" />
                        <Image Grid.Column="1" Width="50" Stretch="Uniform" Source="{Binding Image}" />
                        <TextBlock Grid.Column="2" Text="{Binding Path=ImageFilePath}" />
                        <Grid Grid.Column="3" Visibility="{Binding IsLoading, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, Mode=TwoWay, BindsDirectlyToSource=True, Converter={StaticResource BooleanToVisibilityConverter1}}">
                            <my:LoadingAnimation x:Name="SendAnimation" VerticalAlignment="Center" HorizontalAlignment="Center" />
                        </Grid>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

And here's my BO:

这是我的 BO:

public class Order : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public bool IsSelected { get; set; }
    public string ImageFilePath { get; set; }
    public ImageSource Image { get; set; }

    private bool mIsSending = false;
    public bool IsSending
    {
        get { return mIsSending; }
        set
        {
            mIsSending = value;

            if (PropertyChanged != null)
                PropertyChanged(null, new PropertyChangedEventArgs("IsSending"));
        }
    }
}

And this is how I create the collection and bind it:

这就是我创建集合并绑定它的方式:

    ObservableCollection<Order> mOrders = new ObservableCollection<Order>();

    public MainWindow()
    {
        InitializeComponent();

        lstItems.ItemsSource = mOrders;
    }

回答by dotNET

Nevermind. Sometimes it is that you spend hours digging the problem, finally get frustrated, post it on SO, and the next 2 minutes you figure it out yourself. For any future reader, the only problem was that I was sending nullin PropertyChangedevent. As soon as I changed that to this, things started working like a charm.

没关系。有时是你花了几个小时去挖掘问题,最后感到沮丧,把它贴在 SO 上,接下来的 2 分钟你自己解决了。对于未来的任何读者,唯一的问题是,我是送nullPropertyChanged事件。一旦我将其更改为this,事情就开始像魅力一样发挥作用。