0

I am working on a WPF application using This Design Pattern for MVVM

I have the following 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>

That is bound to this code:

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

However, when I update the TextBox control, the ListView does not refresh on the PropertyChanged Event. Am I missing something in my XAML? I should be able to adjust the selected CreditLimitItem.LimitAmount in my TextBox and the related ListView should update after I click on Adjust. Not sure why it's not working, though.

9
  • 1
    CreditLimitItem needs to be an ObservableObject and you need to raise property changed on bound values Commented Apr 18, 2018 at 13:59
  • 1
    As a note, setting UpdateSourceTrigger=PropertyChanged on the ItemsSource Binding is pointless. It has no effect here, and has nothing to do with the PropertyChanged event of the INotifyPropertyChanged interface. Commented Apr 18, 2018 at 13:59
  • 1
    You'll have to fire a PropertyChanged event for the CreditLimitItem.LimitAmout property. That doesn't happen automagically. Commented Apr 18, 2018 at 14:11
  • 1
    Only if it is supposed to inform about value changes :-) Commented Apr 18, 2018 at 14:13
  • 1
    slight critique, ObservableCollections should be read only properties. They have change notifications used by the UI. That's why they're "observable". Commented Apr 18, 2018 at 15:20

1 Answer 1

1

Your class CreditLimitItem should implement INotifyPropertyChanged for that. When you call _SelectedLimit.LimitAmount = _SelectedLimitAmount; nothing happening as LimitAmount setter does not raise the PropertyChanged event. Note that RaisePropertyChangedEvent("Limits"); does not have effect at all as Limits are still the same collection.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I had assumed that firing off the PropertyChanged event in the Limits Property would change the collection. I've adjusted my properties in my CreditLimitItem class to implement it and it's working now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.