WPF与ElementName的绑定不会更新值

问题描述 投票:0回答:1

我将标签绑定到用户帐户的组合框的SelectedItem。根据您选择的内容,标签会显示该帐户的相应余额。它完美地显示了不同帐户的余额,但是当我检查用户输入的金额不超过所选帐户的余额时,余额永远不会改变。假设Account1的余额为1500,Account2的余额为4000。选择Account1在标签中显示1500,选择Account2在标签中显示4000。

但是无论我选择哪个帐户,我的ViewModel中的Balance属性都保留4000。

我使用Caliburn.Micro来帮助MVVM部分。

我的标签:

<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}"/>

我的组合框

<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}"/>

我的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
c# wpf data-binding caliburn.micro
1个回答
0
投票

在您的组合框中,SelectedItemSelectedAccountNmb,它是一个字符串属性;并且在标签的内容中,您绑定到组合框SelectedItem,即string,没有Balance

您可以这样做:

  public AccountNmb SelectedAccountNmb
    {
        get { return _selectedAccountNmb; }
        set
        {
            _selectedAccountNmb = value;
            NotifyOfPropertyChange(() => SelectedAccountNmb);
            NotifyOfPropertyChange(() => CanMakePayment);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.