开发者

Binding and appendText result to a TextBox

Total beginner in WPF so bear with me.

I building a proof of concept application before I go ahead with it. I have decided to try building it using WPF first.

This is the scenario:

I have:

  • a View=CustomerView (userControl with a button "Buy Products")
  • a txtbox (append all the actions that occurs)
  • ViewModel=CustomerViewModel (talks to the service etc.. and gets results
  • Model =Customer (simple class with properties with InotifyPropertyChanged implemented

Given that I have a collection of products and for each product that I buy I need to APPEND TEXT TO the txtBox in the userControl printing all the actions that occurs, the textBox should look like

Start buying .....
Product 1 Sold
Product 2 Sold
Fineshed....

My problem is that despite I notify successfully after each item is sold I cannot seem to see how I can bind or make "Product 1 Sold,Product 2 appear in the textbox.

In windows forms I would have a userControl with a property called ProductStatus and whenever notified I would appendText to the textBox. "I put beginInvoke as I was getting threadCross operation" when coming from a service. That is another problem that I will have to investigate

private ProductStatus _productStatus;
public ProductStatus Status
{
  get { return _productStatus; }
  set
  {
    _printStatus = value;                 
    BeginInvoke((MethodInvoker)(() => txtProductResult.AppendText(string.Format("Product Sold {0}", Environment.NewLine))));  
  }
}

How do I append text to myTextBox for each item I sell?

=================Added current code===============================

    void LogChanged(object sender, NotifyCollectionChangedEventArgs e) 
        { 
            foreach(object item in e.NewItems) 
            { 
                txtProduct.AppendText(item.ToString()); 
            } 
        } 
        
         <TextBox Margin="12,41,12,59" Name="txtPrintResult" />
       <TextBox Text="{Binding TicketPrintLog, Mode=OneWay}" Margin="12,41,12,12" />
       
       
            ProductModel
            =============
             public string ProductLog{ get; set; }
              
              
            ProductViewModel
            ==================
            public string ProductLog
            {
                get { return _ProductModel.ProductLog; }
            }   
            internal void AppendToProductLogText(string text)
            {
                _ProductModel.ProductLog += text + Environment.NewLine;
                OnPropertyChanged("ProductLog");
            } 
            
            void ProductSoldNotificationReceived(object sender, notificationEventArgs e)
            {
                //THIS FIRES WHENEVER I PRODUCT IS SOLD.
                AppendToProductLogText(e.Message);
             }
            
            ProductView (userControl) XAML
            ================================
            <TextBox Margin="12,41,12,59" Name="txtResult" Text="{Binding ProductLog, Mode=OneWay}" />  
            
            IN CODE BEHIND I DO
              
              public void Action1()
              {
                txtResult.AppendText(_productViewModel.ProductLog);
              }
               public void SellProductAction()
              {
                txtResult.AppendText(_productViewModel.ProductLog);
              }

The problem i have is that i still have todo this

        txtResult开发者_如何学Go.AppendText(_productViewModel.ProductLog); 

which defeats the point on databinding also when executing SellproductAction I only get notified about the last item, it doesn't append Product1 sold, Product 2 sold etc…

Any ideas?


First off, you can still do this the Windows Forms way, by appending the new text to the existing text:

txtProductResult = txtProductResult + Environment.NewLine + "Product sold";

(If you do this from a background thread you will need to use txtProductResult.Dispatcher.BeginInvoke instead of txtProductResult.BeginInvoke.)

However, the idiomatic way to do this in WPF is with data binding. So you would create a ProductSalesLog property on your view model, and bind the TextBox to that property. Then, whenever you update the ProductSalesLog property in the view model, the TextBox will update automatically. Note that your view model must implement INotifyPropertyChanged and must raise the PropertyChanged event for the ProductSalesLog property. Thus:

public class CustomerViewModel : INotifyPropertyChanged
{
  private string _productSalesLog = String.Empty;
  public string ProductSalesLog
  {
    get { return _productSalesLog; }
  }

  internal void AppendSalesLogText(string text)
  {
    _productSalesLog += text + Environment.NewLine;
    OnPropertyChanged("ProductSalesLog");
  }
}

And in your view:

<TextBox Text="{Binding ProductSalesLog, Mode=OneWay}" />


As WPF is a data driven framework, you should have your view databinding to properties on your view model and model. Any changes in the viewmodel or model can then be passed to the view through the INotifyPropertyChanged interface (by raising a propertyChanged event).

This means for most purposes you should not need to access properties on your user control. This should be possible by having a product status on your viewmodel and your text box should bind to this property. You will of course have to make sure when the setter for product status is called that the PropertyChanged event is raised to notify the view to update

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜