[英]DataBinding with read-only property
我有一个允许用户输入付款金额的视图,然后可选择输入本金,利息和托管金额。
使用TwoWay
数据绑定的四个TextBox
就像这样一切都很好:
<TextBlock Grid.Row="3"
Grid.Column="0"
Text="Payment Amount" />
<TextBox Grid.Row="3"
Grid.Column="1"
Text="{x:Bind ViewModel.CurrentPayment.PaymentAmount, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:N}', Mode=TwoWay}" />
<TextBlock Grid.Row="4"
Grid.Column="0"
Text="Principal" />
<TextBox Grid.Row="4"
Grid.Column="1"
Width="100" Text="{x:Bind ViewModel.CurrentPayment.PrincipalAmount, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:N}', Mode=TwoWay}" />
<TextBlock Grid.Row="5"
Grid.Column="0"
Text="Interest" />
<TextBox Grid.Row="5"
Grid.Column="1"
Width="100"
MaxLength="20"
Text="{x:Bind ViewModel.CurrentPayment.InterestAmount, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:N}', Mode=TwoWay}" />
<TextBlock Grid.Row="6"
Grid.Column="0"
Text="Escrow" />
<TextBox Grid.Row="6"
Grid.Column="1"
Width="100"
MaxLength="20"
Text="{x:Bind ViewModel.CurrentPayment.EscrowAmount, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:N}', Mode=TwoWay}" />
但是,当我尝试添加一个名为TotalAmount
的“summary”属性时,它接受PrincipalAmount
+ InterestAmount
+ EscrowAmount
,如下所示:
[JsonIgnore]
public decimal TotalAmount
{
get
{
var amount = EscrowAmount + InterestAmount + PrincipalAmount;
return amount;
}
}
如果用户对TextBox
的主要/利息/托管金额进行了更改,则我的属性不会更新。 加载视图时, TotalAmount
会正确显示,但值会更改时不会更新。 这是TextBlock
的XAML:
<TextBlock Text="{x:Bind ViewModel.CurrentPayment.TotalAmount, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:N}'}" />
有没有办法让TotalAmount
属性知道其他属性之一的值何时发生变化,以便更新显示的值?
编辑
添加其他三个属性定义以显示INotifyPropertyChanged
使用。
private decimal _principalAmount;
[JsonProperty("principalamount")]
public decimal PrincipalAmount
{
get { return _principalAmount; }
set { Set(ref _principalAmount, value); }
}
private decimal _escrowAmount;
[JsonProperty("escrowamount")]
public decimal EscrowAmount
{
get { return _escrowAmount; }
set { Set(ref _escrowAmount, value); }
}
private decimal _interestAmount;
[JsonProperty("interestamount")]
public decimal InterestAmount
{
get { return _interestAmount; }
set { Set(ref _interestAmount, value); }
}
Set
是Template 10的BindableBase
一个方法:
public abstract class BindableBase : IBindable, INotifyPropertyChanged
{
protected BindableBase();
public event PropertyChangedEventHandler PropertyChanged;
public virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null);
public virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression);
public virtual bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null);
public virtual bool Set<T>(Expression<Func<T>> propertyExpression, ref T field, T newValue);
}
编辑
我遇到的主要问题是由于编译绑定的不同行为。 默认Mode
为OneTime
,与OneWay
的标准绑定默认值不同。 一旦我将属性更改通知提交到TotalValue
属性,我仍然必须将绑定模式更改为OneWay
以使我的UI更新。
有关标准绑定和编译绑定的更多信息,请参见x:Bind标记扩展
是的,当这三个属性中的一个发生更改时,您的模型类(Payment)应实现INotifyPropertyChanged并为属性TotalAmount
引发PropertyChanged
事件。
并确保在设置新值后为TotalAmount
引发PropertyChanged
,否则它将不会使用最新值更新TotalAmount
,这可能不是您想要的。
例:
Payment : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private decimal _principalAmount;
public decimal PrincipalAmount
{
get { return _principalAmount; }
set
{
_principalAmount = value;
NotifyPropertyChanged(nameof(PrincipalAmount));
// lets the design know that property "TotalAmount" should also be updated ...
NotifyPropertyChanged(nameof(TotalAmount));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.