ListView不会使用PropertyChanged事件进行更新

时间:2018-04-18 13:52:58

标签: c# wpf xaml mvvm

我正在使用This Design Pattern for MVVM

开发WPF应用程序

我有以下XML

<TextBox Text="{Binding SelectedLimitAmount, UpdateSourceTrigger=PropertyChanged, StringFormat={}{0:$#,#.00}}"
        Grid.Column="1" Grid.Row="0" Height="20" Margin="2,2,2,2"/>
<Button Content="Adjust" 
        FontSize="9"
        Command="{Binding AdjustLimit}"
        Background="Blue" Foreground="White"
        Grid.Column="1" Grid.Row="1" 
        Height="20" Width="75" HorizontalAlignment="Right" Margin="2,2,2,2"/>
<!--Credit Limit Listview-->
<ListView ItemsSource="{Binding Limits, UpdateSourceTrigger=PropertyChanged}" 
          SelectedItem="{Binding SelectedLimit}"
          SelectionMode="Single"
          Grid.ColumnSpan="2" Grid.Row="2" Margin="2,2,2,0">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Account" DisplayMemberBinding="{Binding Activity}"/>
            <GridViewColumn Header="Credit Limit" DisplayMemberBinding="{Binding LimitAmount, StringFormat={}{0:$#,#.00}}"/>
        </GridView>
    </ListView.View>
</ListView>

这与此代码绑定:

Public Class ImportPresenter : ObservableObject
{
    //Credit Limits
    private ObservableCollection<CreditLimitItem> _Limits;
    private CreditLimitItem _SelectedLimit;
    private decimal _SelectedLimitAmount = 0;

    public ObservableCollection<CreditLimitItem> Limits
    {
        get
        {
            return _Limits;
        }
        set
        {
            _Limits = value;
            Debug.Print("Limits Updated");
            RaisePropertyChangedEvent("Limits");
        }
    }

    public decimal SelectedLimitAmount
    {
        get { return _SelectedLimitAmount; }
        set
        {
            _SelectedLimitAmount = value;
            Debug.Print("Amount Changed");
            RaisePropertyChangedEvent("SelectedLimitAmount");
        }
    }

    /// <summary>
    /// Selected Credit Limit
    /// </summary>
    public CreditLimitItem SelectedLimit
    {
        get
        {
            return _SelectedLimit;
        }
        set
        {
            _SelectedLimit = value;
            SelectedLimitAmount = _SelectedLimit.LimitAmount;
            RaisePropertyChangedEvent("SelectedLimit");
        }
    }

    public ICommand AdjustLimit
    {
        get { return new DelegateCommand(UpdateLimit); }
    }

    private void UpdateLimit()
    {
        if(_SelectedLimit != null)
        {
            _SelectedLimit.LimitAmount = _SelectedLimitAmount;
            RaisePropertyChangedEvent("Limits");

            foreach(CreditLimitItem item in Limits)
            {
                Debug.Print(item.LimitAmount.ToString());
            }
        }
    }
}

public class CreditLimitItem
{

    private int _LimitSequence;
    private short _ActivitySequence;
    private string _Activity;
    private decimal _LimitAmount;
    private DateTime _StartDate;
    private DateTime _EndDate;
    private decimal _NewLimit;

    public int LimitSequence { get => _LimitSequence; set => _LimitSequence = value; }
    public short ActivitySequence { get => _ActivitySequence; set => _ActivitySequence = value; }
    public string Activity { get => _Activity; set => _Activity = value; }
    public decimal LimitAmount { get => _LimitAmount; set => _LimitAmount = value; }
    public DateTime StartDate { get => _StartDate; set => _StartDate = value; }
    public DateTime EndDate { get => _EndDate; set => _EndDate = value; }
    public decimal NewLimit { get => _NewLimit; set => _NewLimit = value; }
    public decimal LimitChange { get => _LimitAmount - _NewLimit; }


    public CreditLimitItem(int sequence, short activitySequence, string activity, decimal amount, DateTime startDate, DateTime endDate)
    {
        _LimitSequence = sequence;
        _ActivitySequence = activitySequence;
        _Activity = activity;
        _LimitAmount = amount;
        _StartDate = startDate;
        _EndDate = endDate;
    }
}

但是,当我更新TextBox控件时,ListView事件上的PropertyChanged不会刷新。我在XAML中遗漏了什么吗?我应该能够调整CreditLimitItem.LimitAmount中的所选TextBox,并且点击ListView后相关的Adjust应该会更新。不知道为什么它不起作用。

1 个答案:

答案 0 :(得分:1)

您的类CreditLimitItem应该为此实现INotifyPropertyChanged。 当您调用_SelectedLimit.LimitAmount = _SelectedLimitAmount;没有任何事情发生时,LimitAmount setter不会引发PropertyChanged事件。请注意,RaisePropertyChangedEvent("Limits");根本没有效果,因为限制仍然是相同的集合。