【发布时间】:2019-12-17 09:53:40
【问题描述】:
我将标签绑定到用户帐户的 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
【问题讨论】:
-
不要同时绑定 SelectedItem 和 SelectedValue。仅将 SelectedValue 与 SelectedValuePath 结合使用。请注意,在所有绑定中设置
UpdateSourceTrigger=PropertyChanged是没有意义的。它对像 ItemsSource 那样的 OneWay 绑定没有影响。对于 SelectedValue,默认设置。 -
为什么您希望视图模型中的平衡发生变化?您的标签只显示值,组合框不设置值。还。在 wpf 中使用 bindlinglist 是个坏主意。除非他们最近修复了它们,否则 wpf 使用中存在一些错误。我建议你习惯于使用 observablecollection。
-
@Andy 我明白你的意思,我的错。我现在正在使用 ObserableCollections :)
-
我将如何使用 obserablecollection 中所选帐户的余额值?
-
取决于“使用”的含义。您可以将选定项绑定到一个属性,并通过在 setter 中操作来更改该属性。 social.technet.microsoft.com/wiki/contents/articles/… 或者,如果您实际上是从列表中选择一个值,则可以将 selectedvalue 绑定到其他属性。
标签: c# wpf data-binding caliburn.micro