- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有两个文本框,如果我更改其中一个中的值,则应计算另一个文本框中的值。这需要双向发挥作用。
在我的 ViewModel 中,我有一个名为 NewProductCount 的属性/对象
private ProductCount newProductCount;
public ProductCount NewProductCount
{
get
{
return newProductCount;
}
set
{
newProductCount = value;
if (newProductCount.PackingUnits != 0 || newProductCount.SellingUnits != 0)
{
newProductCount.SellingUnits = (int)newProductCount.PackingUnits * SelectedProduct.PurchasePackaging.UnitsPerPackage;
newProductCount.PackingUnits = newProductCount.SellingUnits / SelectedProduct.PurchasePackaging.UnitsPerPackage;
}
NotifyPropertyChanged();
}
}
在我的 View(xaml) 中,我有一个带有两个文本框的堆栈面板。堆栈面板的数据上下文绑定(bind)到我的 ViewModel 中的 NewProductCount 属性。在这个堆栈面板中,我有两个文本框。第一个绑定(bind)到 NewProductCount 对象的 PackingUnits 属性,第二个绑定(bind)到 NewProductCount 对象的 SellingUnits 属性。现在的问题是,当我更改其中一个文本框中的某些内容时,我想转到 ViewModel 中 NewProductCount 属性的 setter 。
这是我的 View 的样子:
<StackPanel DataContext="{Binding NewProductCount}" >
<Label Content="Number of selling units:"/>
<TextBox Text="{Binding SellingUnits}"/>
<Label Content="Number of packing units"/>
<TextBox Text="{Binding PackingUnits}"/>
</StackPanel>
我还在两个文本框上尝试了 updatesourcetrigger (propertychanged),但这并没有触发我的 ViewModel 中 NewProductCount 属性的 setter 。
提前致谢
阿恩
最佳答案
您可以在 View 模型中订阅 newproductcount 对象的 propertychanged 事件(或您喜欢的任何事件),然后在该对象中的某个属性更改时触发此事件。然后在该事件的处理程序中,您可以在 newproductcount 属性上触发属性更改事件。
更新:基于您只想更新两个文本框的事实,您只需进行几处更改。为清楚起见:
XAML:
<StackPanel DataContext="{Binding NewProductCount}" >
<Label Content="Number of selling units:"/>
<TextBox Text="{Binding SellingUnits, UpdateSourceTrigger=PropertyChanged}"/>
<Label Content="Number of packing units"/>
<TextBox Text="{Binding PackingUnits, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
C#:
public class TestClass
{
public TestClass()
{
NewProductCount = new NewProductCount();
}
public NewProductCount NewProductCount
{
get; set;
}
}
然后:
public class NewProductCount : INotifyPropertyChanged
{
private string _sUnits;
private string _pUnits;
public string SellingUnits
{
get
{
return _sUnits;
}
set
{
_sUnits = value;
_pUnits = string.Empty; //do something dumb just to show the bindings are updating...
NotifyPropertyChanged();
NotifyPropertyChanged("PackingUnits"); //nameof/reflection/whatever you want to use to pass this property name through.
}
}
public string PackingUnits
{
get
{
return _pUnits;
}
set
{
_pUnits = value;
_sUnits = value; //do something dumb just to show the bindings are updating...
NotifyPropertyChanged();
NotifyPropertyChanged("SellingUnits");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName]string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
关于c# - 当对象的属性更改时,WPF 在父级上调用 updatesourcetrigger xaml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43229706/
只是一个简短的问题: 在 wpf 中,如何在代码隐藏中设置和获取文本框的 updatesourcetrigger ? 谢谢 更新: 我遵循 AngleWPF 的代码: var bndE
我正在创建一个带有多个文本框的 WPF 窗口,当用户按下确定按钮时,我希望所有文本框都被评估为非空白。 我知道我必须使用带有'Explicit'的'UpdateSourceTrigger'的TextB
我正在尝试在按下某个键时验证文本框中的文本。这是显示我正在尝试做的事情的最短代码示例: “验证规则”类: class ValidationRules
我最近在 stack overflow 的帮助下解决了一个样式问题...(您可以在我的历史记录中看到问题/答案) 因此,我想到了以下应用于特定文本框的样式。
我正在寻找可以与我的绑定(bind)一起使用的 UpdateSourceTrigger 的自定义版本。我不知道这是否可能,或者相反,我只需要创建自己的绑定(bind)类。我正在寻找的是,而不是 Los
我有一个 TextBlock,与一个对象绑定(bind),当我更新对象的属性时,它没有反射(reflect)在 UI 上,为什么? 代码: 在 Windows1.xaml 中 和 Windows.x
xaml View 模型 public ProductVM() {
我有一个 WPF TextBox,它绑定(bind)到一个对象的(非依赖)属性(实现 INotifyPropertyChanged 以支持绑定(bind))。 验证是通过在绑定(bind)对象上实现
我有一个简单的 Converter,它向 TextBox 中输入的正数添加一个“+”符号。输入数字后,我想启动一些操作,但我不想等到 TextBox 失去焦点:我想在用户输入文本时立即更新绑定(bin
我一直在探索 Caliburn Micro MVVM 框架只是为了感受一下,但我遇到了一些问题。我有一个 TextBox 绑定(bind)到我的 ViewModel 上的字符串属性,我希望在 Text
我有以下 XAML - Start 在 ViewModel 中 - public string this[string columnName] { get {
我正在使用 ValidationRule在 TextBox在我看来,ValidationRule工作正常。 但是,当我处于 Command 的 canExecute 方法的状态时,就会出现问题。 (在
我有以下代码: public partial class Window1 : Window { public Window1() { Initi
MSDN 文档指出: Bindings that are TwoWay or OneWayToSource listen for changes in the target property and
我在 Windows 窗体中托管 WPF 用户控件 在 wpf 用户控件中,我使用来自 wpfToolkit.extended 的时间选择器 如果我使用向上键或向下键,或者只是在文本字段中输入时间,则
我正在使用 Xamarin.Forms,我的 View 完全在 XAML 中创建。我也在使用 Xamarin.Forms.Labs。 我已将 EntryCell 绑定(bind)到这样的属性: 该页
“名称”是在运行时与 TextBlock 绑定(bind)的对象的属性。我已经实现了 INotifyPropertyChanged,所以代码工作正常。我的问题是:如果我将 PropertyChang
我正在努力使用 DataGrid 捕获事件。我想要实现的是,当用户在数据网格单元格的复选框上单击一次时,会触发一个事件,我可以获得当前单元格值。但是,CellChangedEvent 仅在选择更改时触
我正在学习 WPF 教程系列。目前我在这一步 http://www.wpf-tutorial.com/data-binding/responding-to-changes/ 我遇到的问题是,我希望文本
我有两个文本框,如果我更改其中一个中的值,则应计算另一个文本框中的值。这需要双向发挥作用。 在我的 ViewModel 中,我有一个名为 NewProductCount 的属性/对象 priv
我是一名优秀的程序员,十分优秀!