かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[C#][WPF]コマンドですぞ その7「RoutedUICommand」

その6
http://blogs.wankuma.com/kazuki/archive/2008/03/18/128203.aspx

 

RoutedCommandっていうのをやったけど、これを継承してさらに拡張されたコマンドがある!
それが、RoutedUICommand。

このクラスを見てみるとわかるけど、Textプロパティが追加されただけで後は変わらん!という素っ気無い拡張が施されてる。
このTextプロパティって何に役立つのかというと…とりあえずMenuItemとかに関連付けたときに効果が見て取れる。

ということでどうなるか見てみよう。
プロジェクトを新規にWpfUICommandという名前で作る。そして、Window1.xaml.csにHelloとWorldという名前でpublic static readonly RoutedUICommandの定数を作る。コマンドは、Ctrl + HとCtrl + Wに関連付けもしてみよう。

ということで下のようなコードになる。

using System.Windows;
using System.Windows.Input;

namespace WpfUICommand
{
    public partial class Window1 : Window
    {
        public static readonly RoutedUICommand Hello;
        public static readonly RoutedUICommand World;

        static Window1()
        {
            // Helloコマンドの初期化
            Hello = new RoutedUICommand("はろぅ", "Hello", typeof(Window1));
            // Ctrl + Hで動くようにする
            Hello.InputGestures.Add(new KeyGesture(Key.H, ModifierKeys.Control));


            // Worldコマンドの初期化
            World = new RoutedUICommand("わぁるど", "World", typeof(Window1));
            // Ctrl + Wで動くようにする
            World.InputGestures.Add(new KeyGesture(Key.W, ModifierKeys.Control));
        }

        public Window1()
        {
            InitializeComponent();
        }
    }
}

RoutedUICommandのコンストラクタの第一引数がTextプロパティに設定する値になる。残りはRoutedCommandと一緒。
んで、このコマンドをメニューに関連付けてみようと思う。ついでにCommandBindingを使って適当なイベントにも紐付ける。

<Window x:Class="WpfUICommand.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfUICommand="clr-namespace:WpfUICommand"
    Title="Window1" Height="300" Width="300">
    <!-- コマンドとイベントの紐付け -->
    <Window.CommandBindings>
        <CommandBinding Command="{x:Static WpfUICommand:Window1.Hello}" Executed="helloBinding_Executed" />
        <CommandBinding Command="{x:Static WpfUICommand:Window1.World}" Executed="worldBinding_Executed" />
    </Window.CommandBindings>
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="おれおれ">
                <!-- メニューアイテムとコマンドの紐付け -->
                <MenuItem Command="{x:Static WpfUICommand:Window1.Hello}" />
                <MenuItem Command="{x:Static WpfUICommand:Window1.World}" />
            </MenuItem>
        </Menu>
        <Grid>
            
        </Grid>
    </DockPanel>
</Window>

CommandBindingで指定したイベントの定義をWindow1.xaml.csのほうに追加して実装する。とりあえず、ここのコードは本筋じゃないのでMessageBoxではろ~とか表示するだけにしておく。

        private void helloBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("はろ~");
        }
        private void worldBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("わ~るど");
        }

XAMLのほうを見てわかるように、MenuItemはコマンドプロパティを設定してるだけで、何も表示するようには設定してない。
でも、実行してみるとRoutedUICommandのTextプロパティとかInputGesturesに追加したKeyGestureも表示される。
もちろんクリックするとメッセージボックスまで表示されます。
image

うん。いい感じ。

投稿日時 : 2008年3月20日 14:40

Feedback

# [C#][WPF]コマンドですよ その8 「用意されてるコマンド」 2008/03/24 0:37 かずきのBlog

[C#][WPF]コマンドですよ その8 「用意されてるコマンド」

# sacs longchamp 2012/12/15 16:28 http://www.saclongchampachete.info/category/sac-lo

I believe I will certainly visit this place once more soon.

# bags burberry 2012/12/16 5:31 http://www.burberryuksale.info/category/burberry-o

Go by these more aged items and just look for the that tickle your current fancy.

# longchamps 2012/12/16 22:55 http://www.soldesacslongchamp.info/category/sac-lo

Very valuable info. Hope to find more reports soon!

# isabel marant basket 2012/12/17 9:01 http://isabellemarantbaskets.webnode.fr

I am sure I might visit this place yet again soon.

# isabel marant sneakers soldes 2012/12/17 19:30 http://www.isabelmarant-stores.com/category/isabel

make them red with a yellow horse!!

# foulard burberry 2012/12/17 22:05 http://www.sacburberryecharpe.fr/category/echarpe-

Those are incredibly more awesome. Looks similar to klipsch is absolutely made cooperate with iProducts? I have to have android models!

# sacs longchamp 2012/12/17 22:17 http://www.saclongchampachete.info/category/sac-lo

Those are far more awesome. Looks like klipsch is really made cooperate with iProducts? I will want android models!

# burberryukoutlets.wordpress.com 2012/12/19 14:20 http://burberryukoutlets.wordpress.com

I believe I will visit this unique place ever again soon.

# michael kors sac 2012/12/22 19:12 http://michael-kors-canada.webnode.fr/news-/

The fashion don't retract flat and also Philips doesn't supplies a travel pouch within the package.

# Sarenzalando 2013/01/08 3:10 http://www.robenuk.eu/

Accord would be the golden thread that may scarves the very kisses of all of the planet.
Sarenzalando http://www.robenuk.eu/

# chine destock 2013/01/08 7:22 http://www.destockchinefr.fr/maillot/maillot-allem

An actual chum is quite possibly not partner, but nevertheless , partner have been the latest chum.
chine destock http://www.destockchinefr.fr/maillot/maillot-allemagne-pas-cher/

# Hello there! This post could not be written any better! Reading through this post reminds me of my good old room mate! He always kept chatting about this. I will forward this page to him. Fairly certain he will have a good read. Thanks for sharing! 2018/09/17 1:23 Hello there! This post could not be written any be

Hello there! This post could not be written any better!
Reading through this post reminds me of my good old room mate!
He always kept chatting about this. I will forward this
page to him. Fairly certain he will have a good read. Thanks for sharing!

# I am glad to be one of several visitants on this great website (: , regards for putting up. 2018/09/17 8:58 I am glad to be one of several visitants on this g

I am glad to be one of several visitants on this great
website (:, regards for putting up.

# Wow that was odd. I just wrote an incredibly long comment but after I clicked submit my comment didn't appear. Grrrr... well I'm not writing all that over again. Regardless, just wanted to say wonderful blog! 2019/05/03 16:07 Wow that was odd. I just wrote an incredibly long

Wow that was odd. I just wrote an incredibly long comment but after I
clicked submit my comment didn't appear. Grrrr... well I'm not
writing all that over again. Regardless, just wanted to say wonderful blog!

# Have you ever considered about adding a little bit more than just your articles? I mean, what you say is fundamental and all. However just imagine if you added some great images or video clips to give your posts more, "pop"! Your content is exc 2019/05/10 22:06 Have you ever considered about adding a little bit

Have you ever considered about adding a little bit more than just your articles?
I mean, what you say is fundamental and all. However just imagine if you added some great images or video clips to give your posts more, "pop"!
Your content is excellent but with images and clips, this site could certainly be one of the very best in its niche.
Very good blog!

# I know this web site offers quality depending content and extra information, is there any other website which gives these information in quality? 2019/07/11 11:33 I know this web site offers quality depending cont

I know this web site offers quality depending content and extra
information, is there any other website which gives these information in quality?

# Heya i'm for the primary time here. I found this board and I find It truly helpful & it helped me out a lot. I'm hoping to provide one thing again and aid others like you helped me. 2019/08/24 0:42 Heya i'm for the primary time here. I found this

Heya i'm for the primary time here. I found this
board and I find It truly helpful & it helped me out a lot.
I'm hoping to provide one thing again and aid others like
you helped me.

# Fastidious replies in return of this matter with firm arguments and explaining all about that. 2019/10/11 15:53 Fastidious replies in return of this matter with

Fastidious replies in return of this matter with firm arguments and explaining
all about that.

# Fastidious replies in return of this matter with firm arguments and explaining all about that. 2019/10/11 15:54 Fastidious replies in return of this matter with

Fastidious replies in return of this matter with firm arguments and explaining
all about that.

# Fastidious replies in return of this matter with firm arguments and explaining all about that. 2019/10/11 15:55 Fastidious replies in return of this matter with

Fastidious replies in return of this matter with firm arguments and explaining
all about that.

# re: [C#][WPF]コマンドですぞ その7「RoutedUICommand」 2019/11/06 18:17 Rocky Loftings

やあ、\ 本当に\良かったウェブサイト\周りを見回しました。 \する\たかっ| | |必要な私はちょうど\たかった私は、あなたが気にしないことを望むならば、私\ \ \称賛\\あなた\高品質と\願いへあなた\未来へ最高の幸運をあなた\将来的には\\成長、今後数年間で今後\。 \間違いなく \あなたがやっている方法を\見つけるために|発見する|見つけるために再び\すぐ\によって落下|立ち寄っ|おそらく\} \{コールバックすること。私はすぐにここであなたを参照してくださいしなければならない\注意してください乾杯 - Rocky Loftings

# What i don't understood is in truth how you are no longer really a lot more smartly-appreciated than you might be right now. You're so intelligent. You understand therefore significantly relating to this topic, produced me individually consider it from s 2019/11/10 0:36 What i don't understood is in truth how you are no

What i don't understood is in truth how you are no longer really
a lot more smartly-appreciated than you might be right now.

You're so intelligent. You understand therefore significantly relating to this topic,
produced me individually consider it from so many various angles.

Its like women and men aren't involved until it's one thing to accomplish with Lady gaga!
Your individual stuffs outstanding. Always maintain it up!

# What i don't understood is in truth how you are no longer really a lot more smartly-appreciated than you might be right now. You're so intelligent. You understand therefore significantly relating to this topic, produced me individually consider it from s 2019/11/10 0:37 What i don't understood is in truth how you are no

What i don't understood is in truth how you are no longer really
a lot more smartly-appreciated than you might be right now.

You're so intelligent. You understand therefore significantly relating to this topic,
produced me individually consider it from so many various angles.

Its like women and men aren't involved until it's one thing to accomplish with Lady gaga!
Your individual stuffs outstanding. Always maintain it up!

# What i don't understood is in truth how you are no longer really a lot more smartly-appreciated than you might be right now. You're so intelligent. You understand therefore significantly relating to this topic, produced me individually consider it from s 2019/11/10 0:38 What i don't understood is in truth how you are no

What i don't understood is in truth how you are no longer really
a lot more smartly-appreciated than you might be right now.

You're so intelligent. You understand therefore significantly relating to this topic,
produced me individually consider it from so many various angles.

Its like women and men aren't involved until it's one thing to accomplish with Lady gaga!
Your individual stuffs outstanding. Always maintain it up!

# What i don't understood is in truth how you are no longer really a lot more smartly-appreciated than you might be right now. You're so intelligent. You understand therefore significantly relating to this topic, produced me individually consider it from s 2019/11/10 0:39 What i don't understood is in truth how you are no

What i don't understood is in truth how you are no longer really
a lot more smartly-appreciated than you might be right now.

You're so intelligent. You understand therefore significantly relating to this topic,
produced me individually consider it from so many various angles.

Its like women and men aren't involved until it's one thing to accomplish with Lady gaga!
Your individual stuffs outstanding. Always maintain it up!

# I simply could not go away your website before suggesting that I actually loved the usual info a person supply for your visitors? Is going to be again often to check out new posts 2019/11/14 16:55 I simply could not go away your website before sug

I simply could not go away your website before suggesting that I actually loved the usual info a person supply for your visitors?
Is going to be again often to check out new posts

# I was suggested this website by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my difficulty. You are wonderful! Thanks! 2019/11/15 0:11 I was suggested this website by my cousin. I am no

I was suggested this website by my cousin. I am not sure whether this post is written by him as no one else
know such detailed about my difficulty. You are
wonderful! Thanks!

# I'm really loving the theme/design of your website. Do you ever run into any internet browser compatibility problems? A small number of my blog visitors have complained about my blog not working correctly in Explorer but looks great in Chrome. Do you hav 2019/11/23 19:09 I'm really loving the theme/design of your website

I'm really loving the theme/design of your website.
Do you ever run into any internet browser compatibility problems?
A small number of my blog visitors have complained about my blog
not working correctly in Explorer but looks great in Chrome.

Do you have any ideas to help fix this problem?

# Wonderful blog! I found it while searching on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Many thanks 2019/11/24 21:22 Wonderful blog! I found it while searching on Yaho

Wonderful blog! I found it while searching on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?
I've been trying for a while but I never seem to get
there! Many thanks

# Hi there friends, how is everything, and what you would like to say about this piece of writing, in my view its in fact amazing in favor of me. 2019/11/26 3:31 Hi there friends, how is everything, and what you

Hi there friends, how is everything, and what you
would like to say about this piece of writing, in my view its in fact amazing in favor of me.

# It's genuinely very difficult in this full of activity life to listen news on TV, so I only use web for that purpose, and obtain the most up-to-date information. 2019/12/13 3:21 It's genuinely very difficult in this full of act

It's genuinely very difficult in this full of activity life to listen news
on TV, so I only use web for that purpose, and obtain the
most up-to-date information.

# It's genuinely very difficult in this full of activity life to listen news on TV, so I only use web for that purpose, and obtain the most up-to-date information. 2019/12/13 3:22 It's genuinely very difficult in this full of act

It's genuinely very difficult in this full of activity life to listen news
on TV, so I only use web for that purpose, and obtain the
most up-to-date information.

# It's genuinely very difficult in this full of activity life to listen news on TV, so I only use web for that purpose, and obtain the most up-to-date information. 2019/12/13 3:23 It's genuinely very difficult in this full of act

It's genuinely very difficult in this full of activity life to listen news
on TV, so I only use web for that purpose, and obtain the
most up-to-date information.

タイトル  
名前  
Url
コメント