I need help binding a textbox to a class property.....I am working in a WPF application using the MVVM design Pattern.
I have created a few classes to do this.
I have a Customer class
A BaseModel Class that Impliments INotifyPropertyChanged
RelayCommand class that impliments ICommand
and in the XAML I have a datacontext
and this is the textbox XAML
but something is not right..because the property isnt set when I type a value in to the textbox
Im new to MVVM and WPF..so any help would be appreciated
Thank You
-InkedGFX
I have created a few classes to do this.
I have a Customer class
public class Customer : BaseModel { private string _customerName; public string CustomerName { get { return _customerName; } set { _customerName = value; OnPropertyChanged("CustomerName"); } } private string _customerID; public string CustomerID { get { return _customerID; } set { _customerID = value; OnPropertyChanged("CustomerID"); } } }
A BaseModel Class that Impliments INotifyPropertyChanged
public class BaseModel : INotifyPropertyChanged { Customer cust; #region INotifyPropertyChanged Event Handler public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion #region ICommand Methods private ICommand _addNewCustomerCommand; public ICommand AddNewCustomerCommand { get { if (_addNewCustomerCommand == null) _addNewCustomerCommand = new RelayCommand(param => AddNewCustomer(param)); return _addNewCustomerCommand; } set { _addNewCustomerCommand = value; } } private void AddNewCustomer(object param) { cust = new Customer(); XDocument xDoc = XDocument.Load(@"App_Data\Xml\Customer_Data\Customers.xml"); XElement newCust = new XElement("Customer", new XAttribute("Name", cust.CustomerName), new XAttribute("ID", cust.CustomerID)); var lastCust = xDoc.Descendants("Customer").Last(); string newID = lastCust.Attribute("ID").Value + 1; } #endregion }
RelayCommand class that impliments ICommand
public class RelayCommand : ICommand { readonly Action<object> _execute; readonly Predicate<object> _canExecute; public RelayCommand(Action<object> execute) : this(execute, null) { } public RelayCommand(Action<object> execute, Predicate<object> canExecute) { if (execute == null) throw new ArgumentNullException("execute"); _execute = execute; _canExecute = canExecute; } public bool CanExecute(object parameter) { return _canExecute == null ? true : _canExecute(parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { _execute(parameter); } }
and in the XAML I have a datacontext
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModel="clr-namespace:QuickQuote.Model" x:Class="QuickQuote.MainWindow"
xmlns:vm="clr-namespace:QuickQuote.Model"
Title="MainWindow" Height="600" Width="900" WindowStartupLocation="CenterScreen" WindowStyle="None" ResizeMode="NoResize" AllowsTransparency="true" Background="{x:Null}">
<Window.DataContext>
<ViewModel:Customer/>
</Window.DataContext>
and this is the textbox XAML
<TextBox x:Name="txtCompanyName" Text="{Binding Path=CustomerName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="138" Margin="142,307,0,0" Background="#FF323131" SelectionBrush="#FF347DFD">
but something is not right..because the property isnt set when I type a value in to the textbox
Im new to MVVM and WPF..so any help would be appreciated
Thank You
-InkedGFX