gpt4 book ai didi

wpf - 在 WPF MVVM 中绑定(bind)多绑定(bind)文本框

转载 作者:行者123 更新时间:2023-12-03 10:27:49 24 4
gpt4 key购买 nike

我有 3 个文本框与我的类( 事务 )这样的属性绑定(bind)

<TextBox Text="{Binding Path=Transaction.Bills100,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Name="bills100" Grid.Column="2" Grid.Row="1" Margin="7"></TextBox>
<TextBox Text="{Binding Path=Transaction.Bill50,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Name="bills50" Grid.Column="2" Grid.Row="2" Margin="7"></TextBox>
<TextBox Text="{Binding Path=Transaction.Bill20,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Name="bills20" Grid.Column="2" Grid.Row="3" Margin="7"></TextBox>

此外,我还有另一个 TextBox,我在其中完成了多重绑定(bind)并添加了前三个文本框,例如
 <TextBox Grid.Column="2" IsReadOnly="True" Grid.Row="7" Grid.ColumnSpan="2" Margin="7" Name="TotalBills">
<TextBox.Text>
<MultiBinding Converter="{ikriv:MathConverter}" ConverterParameter="x+y+z" Mode="TwoWay">
<Binding Path="Text" ElementName="bills100" />
<Binding Path="Text" ElementName="bills50" />
<Binding Path="Text" ElementName="bills20" />
</MultiBinding>
</TextBox.Text>
</TextBox>

我想将此多绑定(bind)文本框与我的类( Transaction )绑定(bind),其属性为 Transaction.Total,就像我的前三个文本框一样,但它显示错误

Property text is set more than once

最佳答案

实际上,我们不能从一个属性获取双向绑定(bind)的值,然后设置另一个属性的值。
最后我提出了这样的解决方案
在我的类交易中

private double _totalBills; 
public double TotalBills
{
get { return _totalBills; }
set { _totalBills= value; Notify("TotalBills"); }
}

在 XAML 中(而不是多重绑定(bind))
<TextBox Text="{Binding Path=Transaction.TotalBills,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" IsReadOnly="True" Grid.Row="7" Grid.ColumnSpan="2" Margin="7" Name="TotalBills"/>

我的 View 模型
 public class MainViewModel: INotifyPropertyChanged
{
private Transaction _transactionDetails;
public MainViewModel()
{
Transaction= new Transaction();
_transactionDetails.PropertyChanged += _transactionDetails_PropertyChanged;
}
private void _transactionDetails_PropertyChanged(object sender,PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "TotalBills":
_calculate(); //My method for calculation
break;
}
}
}

关于wpf - 在 WPF MVVM 中绑定(bind)多绑定(bind)文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30523750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com