简体   繁体   中英

Bind TextBox multi bind value to another property

this is my entity class

public class Item
{
    private int _stockIn;
    private int _stockOut;
    private int _newStock;

    public int StockIn
    {
        get { return _stockIn; }
        set
        {
            _stockIn = value;
            OnPropertyChanged("StockIn");
        }
    }

    public int StockOut
    {
        get { return _stockOut; }
        set
        {
            _stockOut = value;
            OnPropertyChanged("StockOut");
        }
    }

    public int NewStock
    {
        get { return _newStock; }
        set
        {
            _newStock = value;
            OnPropertyChanged("NewStock");
        }
    }
}

This is my view model class

public class ItemViewModel
{
    private List<Item> _itemCollection;

    public List<Item> ItemCollection
    {
        get { return _itemCollection; }
        set
        {
            _itemCollection = value;
            OnPropertyChanged("ItemCollection");
        }
    }
}

This is xaml

 <DataGrid ItemsSource="{Binding EntityCollection}"> <DataGrid.Columns> <DataGridTemplateColumn Header="New Stock"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox> <TextBox.Text> <MultiBinding Converter="{StaticResource SubstractValuesConverter}"> <Binding Path="StockIn"/> <Binding Path="StockOut"/> </MultiBinding> </TextBox.Text> </TextBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> 

I have bind above ItemCollection list to a data grid. I have a TextBox column in the datagrid and that textbox contains converter to subtract two values (StockIn - StockOut) to get the remaining stock quantity(converter is working ok).

This text box value can be updated.

What I want to do is I want to update the database with the updated text box value. In order to do that I want to bind this textbox value to the NewStock property. How can I do that using MVVM data binding

I think that this is not a good idea to do some "business" logic in converters. You should prepare a better view model. The view model should consist of items with the additional property with remaining stock quantity. I don't know what do you want to achieve, but it looks like you can make some calculation (while retrieving data) to store the subtracted value in NewStock property and just bind it to the TextBox.

Aside from doing calculations when retrieving data, you could also add a new property that is read-only applying the values of both properties of your list object.

public int NetStock
{get { return _stockIn - _stockOut; }}

Since the binding is already to your inventory item, and the other values exist, you are just binding to the one property and shows the net difference (or whatever computations you need). Then do a standard single bind to "NetStock"

HOWEVER, you note this field should be editable, but how for the COMPUTED value. You can't just take and arbitrarily apply the edited value to one or the other. So it would appear your custom binding control allows edit of EITHER the in or the out, which would recompute the total net anyhow. In this scenario, I would then update your other properties to do a RaisePropertyChanged( "NetStock" ) as well, so when either is changed, so too is the NetStock value get refreshed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM