2016-04-04 13 views
0

Ich entwickle ein Verkaufs-Management-System mit MVVM, aber ich habe Probleme, Daten in Sicht zu speichern. Ich nehme an, dass TwoWay-Bindung bedeutet, dass alle in Ansichten eingegebenen Werte an das ViewModel übergeben werden sollen und umgekehrt, habe ich recht?TwoWay Bindung auf Windows Phone 8.1 App, mit MVVM

Ansicht:

<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 verwendet SaveProduct Befehl alle Werte auf der Ansicht eingegeben zu speichern, und es heißt von:

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

I Datacontext auf Code-Behind stablished :

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

Wenn SaveProduct ausgeführt wird, Produkt Klasse zeigt als null, was ist mit meinem Werte ansehen? Was ist falsch an meinem Code?

Dank an alle, die mir hilft, und ich stelle hier meine Repository Link, vielleicht jemand auf meinen Code suchen tief: https://github.com/adoibarra/BillingShop

bearbeiten: Hier ist meine Produktklasse:

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


} 
+0

Wo erstellen Sie ein Produktklassenobjekt? Wenn Sie es nicht instanziieren, ist es null. Bei der bidirektionalen Bindung werden die Eigenschaften Name, Kosten usw. sowohl in Ansichten als auch in Viewmodel aufgefüllt. Aber ich denke, Ihr Product-Objekt selbst ist null, wie wird es seine Eigenschaften füllen? – Archana

+0

Dank @Archana, ich habe meine Frage bearbeitet, um diese Klasse hinzuzufügen, es ist in BusinessLayer-Namespace, ich habe kein Problem, wenn ich Produkte bekomme, aber es ist frustrierend, wenn ich versuche, Werte zu setzen. Irgendeine Idee? –

+0

Nein, ich habe gefragt, wo und wie Sie ein Product-Objekt in Ihrem ProductListViewModel erstellen. – Archana

Antwort

0

Die Lösung war Produkt Eigenschaft innerhalb ProductListViewModel Konstruktor initialisieren:

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

Dank Weimar Yamit Moreno Perez für seine Hilfe und Archana für seine Intervention.

Verwandte Themen