2015-05-29 72 views
0

我想向文本框中添加點擊事件,但我的代碼無法正常工作。單擊文本框時,該事件不會運行。文本框上的點擊事件

這裏是背後

public class Day : INotifyPropertyChanged 
{ 
    public delegate void ClickEventHandler(object sender, ClickEventArgs e); 

    public event Application_IAD.TEST.Day.ClickEventHandler onClick; 

    protected virtual void onTheClick() 
    { 
     if (onClick != null) onClick(this, new ClickEventArgs(new Day())); 
    } 
} 

public class Cal : Control 
{ 
    public ObservableCollection<Day> Days { get; set; } 

    public event EventHandler<ClickEventArgs> DayClick; 

    public Cal() 
    { 
     DataContext = this; 
     CurrentDate = DateTime.Today; 
     DayNames = new ObservableCollection<string> {"Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"}; 

     Days = new ObservableCollection<Day>(); 
     BuildCalendar(DateTime.Today); 
    } 

    public void BuildCalendar(DateTime targetDate) 
    { 
     Days.Clear(); 

     //Show 6 weeks each with 7 days = 42 
     for (int box = 1; box <= 42; box++) 
     { 
      Day day = new Day {Date = d, Enabled = true, IsTargetMonth = targetDate.Month == d.Month}; 
      day.onClick += test; 
     } 
    } 

    private void test(object sender, ClickEventArgs e) 
    { 
     DayClick(this, new ClickEventArgs(sender as Day)); 
    } 
} 

代碼這裏是XAML

<Calen:Cal x:Name="Calendar" Margin="0,50,0,0" DayChanged="Calendar_DayChanged" DayClick="Calendar_DayClick"/> 

我不知道哪裏出錯或不知道怎樣做才能解決它。

回答

0

可以實現WPF TextBox控件(例如TextBox1的)事件訂閱以兩種不同方式,如以下示例代碼段如下所示:

使用C#λ-表達(最短代碼語法):

TextBox1.PreviewKeyDown += (s, e) => { //YOUR CODE}; 

更傳統的方式(長語法):

也有用事件訂閱樣本有關的WPF文本框:

TextBox1.TextChanged += (s, e) => { // YOUR CODE }; 

希望這可能有所幫助。親切的問候,