首页 文章

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

提问于
浏览
2

我有两个文本框,如果我更改其中一个文本框中的值,则应计算另一个文本框中的值 . 这需要在两个方向上工作 .

在我的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();
        }
    }

在我的视图(xaml)中,我有一个带有两个文本框的stackpanel . stackpanel的datacontext与我的ViewModel中的NewProductCount属性绑定 . 在这个stackpanel里面我有两个文本框 . 第一个绑定到NewProductCount对象的PackingUnits属性,第二个绑定到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 .

提前致谢,

阿恩

1 回答

  • 0

    您可以订阅视图模型中newproductcount对象的propertychanged事件(或任何您喜欢的事件),然后在该对象中的某个属性发生更改时触发此事件 . 然后在该事件的处理程序中,您可以在newproductcount属性上触发属性更改事件 .

    Update :基于您只想更新两个文本框的事实,您只需要进行一些更改 . 为清楚起见:

    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));
        }
    }
    

相关问题