【问题标题】:Listbox and observablecollection in wpfwpf中的列表框和可观察集合
【发布时间】:2014-01-22 07:08:33
【问题描述】:

我有显示发票项目的列表框:

        <ListBox x:Name="lbInvoice" ItemsSource="{Binding ocItemsinInvoice}"  Margin="10,27,0,143" Width="412" HorizontalAlignment="Left" BorderBrush="{x:Null}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <ToggleButton x:Name="btnInvoiceItem" Checked="btnInvoiceItem_Checked" Style="{StaticResource InvoiceBG}" TabIndex="{Binding ItemsinInvoiceID}" Padding="20,0,0,0" FontSize="12" Width="408" Height="35" Foreground="#FFcfcfcf" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" BorderThickness="1,0.5">
                        <ToggleButton.BorderBrush>
                            <SolidColorBrush Color="#FF5B616F" Opacity="0.7"/>
                        </ToggleButton.BorderBrush>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Item.ItemName}" Width="200"/>
                            <TextBlock Text="{Binding SalePrice}" Width="50"/>
                            <TextBlock Text="{Binding Quantity,NotifyOnSourceUpdated=True}" Width="50"/>
                            <TextBlock Text="{Binding Total}" Width="50"/>
                        </StackPanel>
                    </ToggleButton>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

问题是当我在后面的代码中更改数量值时(例如点击事件)

ItemsinInvoice _lbi = (ItemsinInvoice)lbInvoice.SelectedItem;
_lbi.Quantity = 99; //for example

屏幕上列表中数量的值没有改变,我错过了什么吗。

谢谢

【问题讨论】:

标签: c# wpf listbox observablecollection


【解决方案1】:

如果您希望对 ViewModel 的更改自动反映在视图中,那么您的类需要实现 INotifyPropertyChanged 并且您需要在每次属性值更改时引发 PropertyChanged 事件:

public class ItemsinInvoice : INotifyPropertyChanged
{
    #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

    private int _quantity;

    public int Quantity
    {
        get { return _quantity; }
        set
        {
            if (_quantity != value)
            {
                _quantity = value;
                OnPropertyChanged("Quantity");
            }
        }
    }
}

更新

我不知道这个代码怎么用,你能解释一下吗?

这是非常简单的代码,但会尝试为您分解它。首先需要实现INotifyPropertyChanged接口

public class ItemsinInvoice : INotifyPropertyChanged

这个接口有一个PropertyChanged 事件,你需要在你的类中发布

public event PropertyChangedEventHandler PropertyChanged;

然后你编写辅助方法来安全地为指定的属性名称引发此事件:

private void OnPropertyChanged(string propertyName)
{
   ...
}

这是INotifyPropertyChanged 实施的。现在您需要在每次属性更改值时使用属性名称调用上述OnPropertyChanged(...) 方法,因此在此示例中,对于public int Quantity { ... },您的调用看起来像这样OnPropertyChanged("Quantity"),您必须记住它区分大小写,因此属性名称必须完全匹配.
通过实现INotifyPropertyChanged 接口并为属性名称引发PropertyChanged 事件,您告诉绑定具有此名称的属性更改了它的值,并且所有相关的绑定都需要刷新。

【讨论】:

  • 我不知道如何使用这个代码,你能解释一下吗?抱歉,我是 INotifyPropertyChanged 的​​新手。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
  • 1970-01-01
  • 2011-02-16
  • 2015-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多