2
votes

I have two textboxes and if I change the value in one of them, the value in the other textbox should be calculated. This need to work in both directions.

In my ViewModel I have a property/object called 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();
        }
    }  

In my View(xaml) I have a stackpanel with two textboxes. The datacontext of the stackpanel has a binding to the NewProductCount property in my ViewModel. Inside this stackpanel I have two textboxes. The first one has a binding to PackingUnits property of the NewProductCount object and the second one has a binding to the SellingUnits property of the NewProductCount object. Now the problem is when I change something in one of the textboxes I want to go to the setter of NewProductCount property in my ViewModel.

This is what my View looks like:

<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>

I have also tried updatesourcetrigger (propertychanged) on the two textboxes but that did not fire the setter of the NewProductCount property in my ViewModel.

Thanks in advance,

Arne

1
It is the setters of the SellingUnits and PackingUnits properties that should get hit when you type into the TextBoxes, not the NewProductCount property. There is no PropertyChanged event raised for this one.mm8
I think you are confused about how the NewProductCount is used. You're currently binding it as the DataContext for a StackPanel, so how is it getting set? Do the SellingUnits and PackingUnits properties fire PropertyChanged events? Does you ProductCount class implement INotifyPropertyChanged interface? Also all of your TextBox bindings should be Mode=TwoWay to handle changes to the model properties and user input changes.Pedro Silva

1 Answers

0
votes

You could subscribe to the propertychanged event (or any event you like, for that matter) of the newproductcount object in your view model then fire this event when one of the properties in that object change. Then in the handler for that event you could fire the property changed event on your newproductcount property.

Update: Based on the fact you just want to update both text boxes you only need to make a couple of changes. For clarity:

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

And then:

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