Author Topic: Do you think I can have a little help with changing the code slightly ?  (Read 10044 times)

lemmings

  • Full Member
  • ***
  • Posts: 165
I have been trying to edit the logic around the change price (so that I can remove the Big Numerator).  I have been able to create a decimal value after an update button is clicked in SelectedTicketItemsView.xaml but I cannot seem to transfer that value to either the UpdatePrice method in TicketItemViewModel or the SelectedTicketItemViewModel.

Everything else works, press change price, numeric keypad pops up (which I created), a textbox is displayed with the current price, you enter the new price, click update and now I have been frustrated - I cannot get the value entered in the text box to been seen in either model above so I can Update the price.

I not familiar with the WPF so Im learning as I go (plus I'am more a VB programmer so I'am also learning C# - although thats not too bad).

Is it possible that one of the developers can just point me in the right direction.  Thank You

emre

  • SambaPOS Developer
  • Samba Team
  • Hero Member
  • *****
  • Posts: 1564
Re: Do you think I can have a little help with changing the code slightly ?
« Reply #1 on: February 21, 2013, 07:19:34 pm »
To be able to implement such things in SambaPOS you need to get how Event Aggregation mechanism works. It is very easy.

You can publish an event like this.

SelectedTicketItem.PublishEvent("ChangeMyPrice");

Now somebody should listen this event and process it. You can register a listener as:

EventServiceFactory.EventService.GetEvent<GenericEvent<TicketItemViewModel>>().Subscribe(OnTicketItemViewModelEvent);

OnTicketItemViewModelEvent method signature should be:

void OnTicketItemViewModelEvent(EventParameters<TicketItemViewModel> obj){}

Since SelectedTicketItem type is TicketItemViewModel it will catch this event. We define it with GenericEvent type.

The trick is, when we call "ChangeMyPrice" event we also pass SelectedTicketItem instance to the event. So inside event listener method you can access SelectedTicketItem with obj.Value. Also obj.Topic should be "ChangeMyPrice"

so we can implement OnTicketItemViewModelEvent as :

void OnTicketItemViewModelEvent(EventParameters<TicketItemViewModel> obj){
   if(obj.Topic == "ChangeMyPrice") // This is the string value we used to identifty the event we call.
  {
     var sti = obj.Value; // obj.Value is SelectedTicketItem because we called this event through its instance.
     var newPrice = InteractionService.AskPrice(sti.Price); // User Enters Price Here
     sti.UpdatePrice(newPrice);
     sti.PublishEvent("PriceUpdated");
  }
}

At the end we published a different event to let other parts of the application knows "ticket item price changed". We also passed sti instance as the event value parameter. That means whenever we need to do something with TicketItemViewModel we need to pass it as a parameter so the event listener can know which TicketItemViewModel it should work with.

I know this is not the implementation of your issue because you don't ask price through InteractionService. On your example you'll handle this event, keep SelectedTicketItem somewhere and display that screen. Update command will update the price and publish "PriceUpdated" event. Most probably you should handle "PriceUpdated" event somewhere in TicketListViewModel and refresh values like we do in current OnChangePrice method.

Compared to classic windows programming what we are doing may seem a little weird. In fact this is not directly related with WPF. WPF is mostly XAML part. If you are interested you can read more about MVVM Pattern, Command Pattern and Event Aggregator Pattern. You'll find a lot of information via Google.

You can create a SambaPOS-2 fork on github and submit your code there. I think I can help better on code.

lemmings

  • Full Member
  • ***
  • Posts: 165
Re: Do you think I can have a little help with changing the code slightly ?
« Reply #2 on: February 22, 2013, 02:13:15 pm »
Thanks Emre,

I did some more reading on WPF, XAML, Data Binding and Command Parameters and have now resolved the issue.  The price is being updated correctly on all tickets and if a zero amount is entered I get the pop-up confirming that zero prices are not allowed and the price does not get changed.

Now to see if I can move the Find Ticket button to the Table Screen and see if I can change my numerator keypad so that it moves into focus from the right-hand side.