gpt4 book ai didi

c# - 当对象的属性更改时,WPF 在父级上调用 updatesourcetrigger xaml

转载 作者:太空宇宙 更新时间:2023-11-03 15:06:31 25 4
gpt4 key购买 nike

我有两个文本框,如果我更改其中一个中的值,则应计算另一个文本框中的值。这需要双向发挥作用。

在我的 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/

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