かずきの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?

タイトル  
名前  
Url
コメント