[英]WPF Binding with ElementName doesn't update the value
我將一個 Label 綁定到用戶帳戶的 ComboBox 的 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}"/>
我的視圖模型
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
在您的 Combobox 中, SelectedItem
是SelectedAccountNmb
,它是一個字符串屬性; 並且在您的 Label 內容中,您將綁定到您的 Combobox SelectedItem
,它是string
並且沒有Balance
。
你可以這樣做:
public AccountNmb SelectedAccountNmb
{
get { return _selectedAccountNmb; }
set
{
_selectedAccountNmb = value;
NotifyOfPropertyChange(() => SelectedAccountNmb);
NotifyOfPropertyChange(() => CanMakePayment);
}
}
而且您不需要在 Combobox 中綁定SelectedValue
,因為您可以從SelectedAccountNmb
屬性中獲取值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.