简体   繁体   中英

TwoWay Binding on Windows Phone 8.1 app, using MVVM

I'm developing a sales management system using MVVM , but I have problems to save data entered on view. I'm assuming that TwoWay Binding mean that all values entered on views should be passed to the ViewModel and vice-versa, am I right?

View :

 <TextBox x:Name="NameText"
             Grid.ColumnSpan="2"
             Grid.Row="2"
             Header="Nombre:"
             Style="{StaticResource RegisterTextBoxStyle}"
             Text="{Binding Product.Name, Mode=TwoWay}"/>
    <ComboBox x:Name="UnitCombo"
              Grid.Row="3"
              Grid.ColumnSpan="2"
              Header="Unidad:"
              PlaceholderText="Elige la medida"
              Style="{StaticResource RegisterComboBoxStyle}"
              ItemsSource="{Binding Path=UnitsSource, Mode=OneWay}"
              SelectedValue="{Binding SelectedUnit, Mode=TwoWay}"/>
    <TextBox x:Name="CostText"
             Grid.Column="0"
             Grid.Row="4"
             Header="Costo:"
             Style="{StaticResource RegisterTextBoxStyle}"
             Text="{Binding Product.Cost, Mode=TwoWay}"/>
    <TextBox x:Name="PriceText"
             Grid.Column="1"
             Grid.Row="4"
             Header="Precio:"
             Style="{StaticResource RegisterTextBoxStyle}"
             Text="{Binding Product.Price, Mode=TwoWay}"/>
    <ToggleSwitch x:Name="ActiveToggle"
                  Grid.Column="1"
                  Grid.Row="5"
                  Style="{StaticResource RegisterToggleSwithStyle}"
                  IsOn="{Binding Product.Active, Mode=TwoWay}"/>

View uses SaveProduct command to save all the values entered on the view, and it's called from:

        <AppBarButton Icon="Accept" Label="Añadir" Command="{Binding Path=SaveCommand}"/>

I stablished DataContext on codebehind:

public AddProduct()
    {
        InitializeComponent();
        DataContext = new ProductListViewModel();
    }

ProductListViewModel :

namespace BillingShop.ViewModel
{
    public class ProductListViewModel : ViewModelBase, INavigable
    {
        public ObservableCollection<ProductViewModel> Items { get; private set; }
    private DelegateCommand _saveProduct;
    public bool IsUpdating { get; set; }
    public ProductViewModel Product { get; set; }
    public Visibility UpdatingVisibility => (IsUpdating || Items == null || Items.Count == 0) ? Visibility.Visible : Visibility.Collapsed;

    public ProductListViewModel()
    {
        if (IsInDesignMode)
        {
            return;
        }
        _saveProduct = new DelegateCommand(SaveCommand_Executed);
    }

    #region Product Members

    private string _unit;
    public string SelectedUnit
    {
        get { return _unit; }
        set
        {
            _unit = value;
            OnPropertyChanged();
        }
    }

    #endregion


    public IEnumerable<RegisteredUnits> UnitsSource => Enum.GetValues(typeof(RegisteredUnits)).Cast<RegisteredUnits>();

    public ICommand SaveCommand => _saveProduct;

    private void SaveCommand_Executed()
    {
        var product = new Product
        {
            Name = Product.Name,
            Unit = Product.Unit,
            Cost = Convert.ToDouble(Product.Cost),
            Price = Convert.ToDouble(Product.Price),
            Active = Convert.ToBoolean(Product.Active)
        };
        ProductManager.SaveProduct(product);
    }

    public void PopulateProductViewModel(ProductViewModel entry)
    {
        Product = entry;
        OnPropertyChanged("Product");
    }
    public Product GetProduct()
    {
        return Product?.GetProduct();
    }
}

}

ProductViewModel :

public class ProductViewModel : ViewModelBase
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Unit { get; set; }
    public double Cost { get; set; }
    public double Price { get; set; }
    public bool Active { get; set; }
    public List<SalesDetails> SalesDetail { get; set; }

    public ProductViewModel()
    {
    }

    public ProductViewModel(Product item)
    {
        Update(item);
    }
    public void Update (Product item)
    {
        Id = item.ID;
        Name = item.Name;
        Unit = item.Unit;
        Cost = item.Cost;
        Price = item.Price;
        Active = item.Active;
        SalesDetail = item.SalesDetail;
    }

    public Product GetProduct()
    {
        return new Product
        {
            ID = Id,
            Name = Name,
            Unit = Unit,
            Cost = Cost,
            Price = Price,
            Active = Active,
            SalesDetail = SalesDetail
        };
    }
}

When SaveProduct is executed, Product class shows as null, what about my view values? What is wrong with my code?

Thanks to everyone who helps me, and I'm putting here my repository link, maybe someone wants to look deep on my code: https://github.com/adoibarra/BillingShop

Edit : Here's my Product class :

public class Product : IBusinessEntity
{
    /// <summary>
    /// Represents a Product.
    /// </summary>
    public Product()
    {
    }


    /// <summary>
    /// Get or sets the product identifier.
    /// </summary>
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    /// <summary>
    /// Get or sets the product name.
    /// </summary>
    [MaxLength(40)]
    public string Name { get; set; }
    /// <summary>
    /// Get or sets if the product can be measured in units or kilograms.
    /// </summary>
    public string Unit { get; set; }
    /// <summary>
    /// Get or sets the product cost.
    /// </summary>
    public double Cost { get; set; }
    /// <summary>
    /// Get or sets the product price.
    /// </summary>
    public double Price { get; set; }
    /// <summary>
    /// Get or sets the product state.
    /// </summary>
    public bool Active { get; set; }
    /// <summary>
    /// One-To-Many relationship with SalesDetails.
    /// </summary>
    [OneToMany(CascadeOperations = CascadeOperation.All)]
    public List<SalesDetails> SalesDetail { get; set; }


}

The solution was initialize Product property inside ProductListViewModel constructor:

public class ProductListViewModel
{
   public ProductListViewModel ()
   {
      Product = new ProductViewModel();
   }
   .
   .
   .
}

Thanks to Weimar Yamit Moreno Perez for his help, and Archana for his/her intervention.

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