How to Bind String Value to TextBox?

Jun 3, 2013 at 11:29 PM
Hi

I want to bind a string value to a textbox that I can grab out of the object when I click on my button(save)

I have this

public class Product
{
public string Name {get; set;}
public decimal Price {get; set;}
}

public Product Product{ get; set; }

I am not sure if I need some sort of PropertyChangeEvent. I also don't see the binding in the textbox in blend4.

I am making WP7 application.
Jun 4, 2013 at 11:49 AM
Is your Product class a ViewModel? If so, it should derive from ViewModelBase.
Then, your Name setter should look something like:
            set
            {
                if (Name == value)
                {
                    return;
                }
                Name = value;
                RaisePropertyChanged(() => Name);
            }
If your textbox's Text property is bound to the Name property, the textbox should update when the Name property changes.
Could you give more details?
  • How have you set the DataContext for the textbox?
  • What happens when the button is clicked?
Jun 4, 2013 at 4:18 PM
Edited Jun 4, 2013 at 4:24 PM
ccbVT wrote:
Is your Product class a ViewModel? If so, it should derive from ViewModelBase.
Then, your Name setter should look something like:
            set
            {
                if (Name == value)
                {
                    return;
                }
                Name = value;
                RaisePropertyChanged(() => Name);
            }
If your textbox's Text property is bound to the Name property, the textbox should update when the Name property changes.
Could you give more details?
  • How have you set the DataContext for the textbox?
  • What happens when the button is clicked?
I was thinking it was the Domain class. I am trying to go through the MIX11 video and I would think that my Product class would be my Domain class like the Friend Class is in the video.

and this would be in my View Model class

public Product Product{ get; set; }


How I have it right now I don't seem to have a DataContext on the textbox(I do have it in my layOutRoot)). What I did was like in the video. I opened blend up and dragged my VieModelLocator onto the layoutroot and then I dragged the property from the product class onto a textbox I made.
        <TextBox Text="{Binding Product.Name, Mode=TwoWay}" Margin="257,211,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="80"/>
When I click the button I made a relay command that I grabbed off in some old book I had
        public RelayCommand AddAVendorCommand
        {
            get;
            private set;
        }
            AddAVendorCommand = new RelayCommand(() => AddVendor()); // in constructor of ViewModel

Right now it just goes to the AddVendor method and I have a break point to see if what I put in the textbox show sup in the Product object.