使用 ElementName 的 WPF 绑定不会更新值



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

但是,无论我选择哪个帐户,我的 ViewModel 中的余额属性都保持 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}"/>

我的视图模型

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

在你的组合框中,SelectedItemSelectedAccountNmb它是一个字符串属性;在你的标签的内容中,你绑定到你的组合框SelectedItemstring的,没有Balance

您可以这样做:

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

而且您不需要在组合框中绑定SelectedValue,因为您可以从SelectedAccountNmb属性中获取值

最新更新