Menu

MVVM将文本框绑定到WPF中的类福彩12选5走势图

墨GFX

知名会员
已加入
2013年2月2日
留言内容
142
编程经验
Beginner
我需要帮助将文本框绑定到类福彩12选5走势图.....我正在使用MVVM设计模式在WPF应用程序中工作。
我创建了一些课程来做到这一点。

我有一个客户班

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


    }


隐含INotifyPropertyChanged的BaseModel类

 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
   }

隐含ICommand的RelayCommand类

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


在XAML中,我有一个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>

这是文本框XAML
<TextBox x:Name="txtCompanyName" Text="{Binding Path = CustomerName,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}"Horizo​​ntalAlignment ="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="138" Margin="142,307,0,0" Background="#FF323131" SelectionBrush="#FF347DFD">


但是有些不正确..因为在文本框中键入值时福彩12选5走势图未设置

我是MVVM和WPF的新手,因此将不胜感激

谢谢你
-InkedGFX
 
最佳 底部