简体   繁体   中英

WPF Binding with ElementName doesn't update the value

I bind a Label to the SelectedItem of a ComboBox of the user's accounts. Depending on what you select, the label shows the corresponding balance of that account. It shows the balance of the different accounts just perfectly, but when i check that the amount, the user types in, doesn't exceed his balance of selected account, the balance never changes. Let's say Account1 has a balance of 1500 and Account2 has a balance of 4000. Selecting Account1 shows 1500 in the label, selecting Account2 shows 4000 in the label.

But the Balance property in my ViewModel stays 4000 no matter which account i select.

I use Caliburn.Micro to help with the MVVM part.

My Label:

<Label x:Name="Balance"
                   Content="{Binding ElementName=Sender, Path=SelectedItem.Balance, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                   DockPanel.Dock="Right"
                   HorizontalContentAlignment="Right"
                   Language="da-DK"
                   ContentStringFormat="{}{0:C}"
                   Style="{StaticResource InputBoxPayments}"/>

My ComboBox

<xctk:WatermarkComboBox x:Name="Sender"
                                    ItemsSource="{Binding Sender, UpdateSourceTrigger=PropertyChanged}"
                                    SelectedItem="{Binding SelectedAccountNmb, Mode=OneTime, UpdateSourceTrigger=PropertyChanged}"
                                    SelectedValue="{Binding Path=SelectedAccountNmb, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                    SelectedValuePath="AccountNmb"
                                    DisplayMemberPath="AccountName"
                                    DockPanel.Dock="Right"
                                    Watermark="Vælg din konto..."
                                    Style="{StaticResource InputBoxPayments}"/>

My ViewModel

        private string _accountNmb;
        private string _accountType;
        private string _accountName;
        private decimal _balance;
        private BindingList<AccountModel> _sender = new BindingList<AccountModel>();
        private string _selectedAccountNmb;
        private decimal _amount;
        private string _note;
        private string _receiver;

        public string AccountNmb
        {
            get { return _accountNmb; }
            set
            {
                _accountNmb = value;
                NotifyOfPropertyChange(() => AccountNmb);
            }
        }
        public string AccountType
        {
            get { return _accountType; }
            set
            {
                _accountType = value;
                NotifyOfPropertyChange(() => AccountType);
            }
        }
        public string AccountName
        {
            get { return _accountName; }
            set
            {
                _accountName = value;
                NotifyOfPropertyChange(() => AccountName);
            }
        }
        public decimal Balance
        {
            get { return _balance; }
            set
            {
                _balance = value;
                NotifyOfPropertyChange(() => Balance);
                NotifyOfPropertyChange(() => CanMakePayment);
            }
        }
        public BindingList<AccountModel> Sender
        {
            get { return _sender; }
            set
            {
                _sender = value;
                NotifyOfPropertyChange(() => Sender);
                NotifyOfPropertyChange(() => CanMakePayment);
            }
        }
        public string SelectedAccountNmb
        {
            get { return _selectedAccountNmb; }
            set
            {
                _selectedAccountNmb = value;
                NotifyOfPropertyChange(() => SelectedAccountNmb);
                NotifyOfPropertyChange(() => CanMakePayment);
            }
        }
        public decimal Amount
        {
            get { return _amount; }
            set
            {
                _amount = value;
                NotifyOfPropertyChange(() => Amount);
                NotifyOfPropertyChange(() => CanMakePayment);
            }
        }
        public string Note
        {
            get { return _note; }
            set
            {
                _note = value;
                NotifyOfPropertyChange(() => Note);
                NotifyOfPropertyChange(() => CanMakePayment);
            }
        }
        public string Receiver
        {
            get { return _receiver; }
            set
            {
                _receiver = value;
                NotifyOfPropertyChange(() => Receiver);
                NotifyOfPropertyChange(() => CanMakePayment);
            }
        }

        public bool CanMakePayment
        {
            get
            {
                bool output = false;

                if (SelectedAccountNmb?.Length > 0 &&
                    Note?.Length > 0 &&
                    Receiver?.Length > 0 &&
                    SelectedAccountNmb != Receiver &&
                    Amount > 0 &&
                    Amount <= Balance) // This is where i make sure the amount the user types in doesn't exceed his balance.
                {
                    output = true;
                }

                return output;
            }
        }

// Omitted rest of the file

In your Combobox, SelectedItem is SelectedAccountNmb that it is a string property; and in your Label's content you're binding to your Combobox SelectedItem that is string and no have Balance .

You can do this:

  public AccountNmb SelectedAccountNmb
    {
        get { return _selectedAccountNmb; }
        set
        {
            _selectedAccountNmb = value;
            NotifyOfPropertyChange(() => SelectedAccountNmb);
            NotifyOfPropertyChange(() => CanMakePayment);
        }
    }

And you don't need to bind SelectedValue in your Combobox because you can get value from SelectedAccountNmb property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM