Skip to content

Adding Event Handlers in VS for WPF

September 13, 2006

"Cider" has yet to make it easy to add a click handler for a button in the style that most VB and WinForms developers are accustomed to.

Update: Beta 2 of VS 2008 added support for this (7/2007).  See Andrej Tozon’s post.

I list the best techniques you can use until the real support is in VS for both VB & C# Developers.
Please comment if you have other tips…

VB Technique

1) Add an element to the window1.xaml file, give it a Name

<Button Name="button1">Ok</Button>

2) Build (ctrl-shift-b)

This will compile the xaml file and create a window1.g.vb file in obj\debug\ that includes a partial class definition with the following line:

dim button1 as System.Windows.Controls.Button;

Now you’ll be able to use that button from code-intellisense.

3) Open the code behind file (Window1.xaml.vb)

4) Use normal drop down to choose button1 and the click event.

The handler is created for you.

 

C# Technique

1) Add an element to the window1.xaml file, give it a Name

<Button Name="button1">Ok</Button>

2) Build (ctrl-shift-b)

This will compile the xaml file and create a window1.g.cs file in obj\debug\ that includes a partial class definition with the following line:

System.Windows.Controls.Button button1;

Now you’ll be able to use that button from code-intellisense.

3) Go to the constructor for Window1 in Window1.xaml.cs and type:

button1.Click += [Tab][Tab]

(press the tab key instead of typing "[tab]")

4) Voilà

Your file will now contain:

public Window1()
{
    InitializeComponent();
    button1.Click += new RoutedEventHandler(button1_Click);
}

void button1_Click(object sender, RoutedEventArgs e)
{
    throw new Exception("The method or operation is not implemented.");
}

From → Tools

14 Comments
  1. Robert permalink

    1) Add an element to the window1.xaml file, give it a Name and add Click="button1_Click"

    <Button Name="button1">Ok</Button>
    2) Build (ctrl-shift-b)

    This will compile the xaml file and create a window1.g.cs file in obj\\debug\\ that includes a partial class definition with the following line:
    System.Windows.Controls.Button button1;
    Now you\’ll be able to use that button from code-intellisense.
    3) Add Click="button1_Click" to xaml definition, hover over the smart tag that appears and click on "Add Eventhandler button1_Click" 

    <Button Name="button1" Click="button1_Click">Ok</Button>
    4) Voilà
    InitializeComponent() in the window1.g.cs file will now contain:
    button1.Click += new RoutedEventHandler(button1_Click);and window1.xaml.cs will now contain

    public Window1(){}
    void button1_Click(object sender, RoutedEventArgs e){    throw new Exception("The method or operation is not implemented.");}
     
    (I wish)

  2. Neal Hudson permalink

    Nice one for this little bit of code – worked first time round for me!

  3. Unknown permalink

    Thank you.  I know there had to be some automatic way to add a button click event.  I have 30 years experience, just coming up to speed with WPF, and could not figure out how to do this.  I cannot believe this was not in the first version of the product.  I am not impressed with WPF.  I saw it 10 years ago, except it was called Java Swing (and it was easy to use).

  4. Suzanne permalink

    You can add event handlers in the exact same way in NET 2.0, but not only that, NET 2.0 has an event handlers window where all you need to do is select a control and double click on the desired event handler to have one automatically generated. I really can\’t understand why NET 3.0 and 3.5 would make little (very commonly used things) harder to use. I agree with (no name) WPF is not impressive. I would go as far to say that it is a step back from NET 2.0. Yes it is visually more appealing, but NET 2.0 is by far easier to use, easier to code, contains more usable controls, with greater range of options and is generally more robust.

  5. Unknown permalink

    lotro gold lord of the rings gold lord of the rings online gold lotro gold lord of the rings gold lord of the rings
    online gold Warhammer gold
    Warhammer online gold Warhammer money War gold War money Tabula Rasa Credit lotro gold lord of the rings gold lord of the rings online gold lord of the
    rings online gold lord of the rings
    gold Tabula Rasa Credit World of Warcraft gold PotBS Doubloon Pirates of the
    Burning Sea Doubloon PotBS Gold Pirates of the Burning Sea
    Gold lotro gold lord of the rings gold lord of the rings online gold Tabula Rasa Credit Warhammer gold Warhammer online gold PotBS Doubloon PotBS gold Pirates of the Burning Sea
    gold Pirates of the
    Burning Sea Doubloon

  6. Unknown permalink

    ECサイト構築   ウィークリーマンション
    治験 結婚指輪
    太陽光発電 結婚式 演出 吉原 ソープ
    債務整理
     害虫駆除 法律事務所 求人 看護師
     キャッシング
    転職 バイアグラ ショッピング枠 現金化  釣り 釣具
    別れさせ屋 価格   賃貸 
    不動産担保ローン  介護 看護師   会社設立   横浜中華街 ウェディングドレス  ウェディング
    カラコン 
    メル友 ウィークリーマンション
    自動車ガラス  不動産 ダイビング
     葬儀 東京ショッピング枠 現金化  データ復旧  テレマーケティング  RAID復旧 データ復元 データ復旧

      

  7. Unknown permalink

     ショッピング枠 現金化 RAID復旧 データ復元  データ復旧 手掌多汗症 手 汗   メタボ対策  データ復旧  不動産担保ローン 海外推广  国际推广   网络营销  
    网络推广 バイアグラ  横浜 賃貸  データ復旧   RMT 価格   賃貸 カラコン 

     

  8. Unknown permalink

    結婚式 演出
    太陽光発電 介護 転職
     会社設立データ復旧   釣り 釣具
    ウィークリーマンション  株式投資 害虫駆除  看護師  キャッシング テレマーケティング治験
    バイアグラ 看護師   横浜中華街
    結婚指輪  ウェディング  メル友
    ウィークリーマンション 不動産

  9. Unknown permalink

    男性下着   吉原 ソープ  威龍 デリヘル 勃起 不全 
    デリバリーヘルス 
     

     精力剤 インポ  すすきの デリヘル 人妻 風俗 精力剤  勃起薬 熟女

    横浜デリヘル 
    品川デリヘル
       すすきの 風俗  
    横浜デリヘル 
       包茎手術

  10. Unknown permalink

    子宮筋腫  フローリング オーク 投資
    お取り寄せグルメ

     

  11. Unknown permalink

    メタボ対策   横浜中華街 転職 RAID復旧 データ復元  データ復旧 データ復旧  ショッピング枠 現金化  横浜 賃貸  データ復旧  害虫駆除  太陽光発電 不動産担保ローン  FX  横浜中華街

  12. Unknown permalink

    アダルトグッズ アダルトグッズ アダルトグッズ アダルトグッズ 大人のおもちゃ 大人のおもちゃ 大人のおもちゃ 大人のおもちゃ 

  13. Unknown permalink

    エスピーエフ 花粉症  新宿賃貸    マンスリーマンション     営業代行 印鑑 営業支援  雛人形  フレッツ光  人材派遣  マンション 投資    マンション投資   オートローン   ゴルフ会員権   バイク便派遣   投資マンション鼻づまり  Bフレッツ  FX  いびき  販売促進

  14. Unknown permalink

    Welcome to enter (wow gold) and (wow power leveling) trading site, (Rolex) are cheap, (World of Warcraft gold) credibility Very good! Quickly into the next single! Key words directly to the website click on transactions! -144799137274628

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: