【发布时间】:2019-05-10 09:27:15
【问题描述】:
这是一个完整的菜鸟问题,我似乎无法弄清楚。我有一个文本框和一个文本块。文本框应该是存款金额,文本块应该是存款后的余额。它基本上只是将存款箱中的值添加到新余额框中的任何值中,但我需要在用户键入时完成此数学运算。基本上,当用户在存款文本框中键入时,我希望将存款中的值添加到存款后文本块中的值中。这不会更新,如果我通过调试器运行它,它不会在用户键入时停止程序,所以我知道它没有识别出属性正在被更改,但我似乎无法理解为什么.我有 datacontext 集,我知道它绑定没有绑定错误,因为绑定适用于常规文本,所以我觉得没有必要在这里展示。我怀疑这只是我的数学和绑定的简单事情。 文本框和文本块的代码在这里:
<!-- TextBlock and TextBox for deposit -->
<TextBlock Text="Deposit:" TextAlignment="Center" VerticalAlignment="Center" FontSize="30" FontWeight="Bold"/>
<TextBox Text="{Binding DepositAmount, UpdateSourceTrigger=PropertyChanged}" BorderBrush="Black" BorderThickness="3" Padding="3" Margin="-60 15 30 10" Grid.Column="1"/>
<!-- TextBlock and TextBox for new balance -->
<TextBlock Text="New Balance:" Margin="5 0 0 0" TextAlignment="Left" VerticalAlignment="Center" FontSize="28" FontWeight="Bold" Grid.Row="1"/>
<TextBox Text="{Binding BalanceAfterDeposit, UpdateSourceTrigger=PropertyChanged}" FontWeight="Bold" IsReadOnly="True" BorderBrush="Black" BorderThickness="3" Padding="3" Margin="-60 15 30 10" Grid.Column="1" Grid.Row="1"/>
这里有两个属性:
public double DepositAmount
{
get => this._depositAmount;
set
{
if (this._depositAmount != value)
{
this._depositAmount = value;
OnPropertyChanged(nameof(DepositAmount));
}
}
}
public double BalanceAfterDeposit
{
get => this._balanceAfterDeposit + _balance;
set
{
if ((this._balanceAfterDeposit + _balance) != value)
{
this._balanceAfterDeposit = value;
OnPropertyChanged(nameof(BalanceAfterDeposit));
}
}
}
这可能是一个非常愚蠢的数学错误,但我们将不胜感激!如果您对我的代码有任何建议,那也会很有帮助。谢谢!
【问题讨论】:
标签: c# wpf data-binding inotifypropertychanged propertychanged