かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

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

書庫

日記カテゴリ

[WPF][Silverlight]XAML Power Toys v5.0が出てました

ふらっと覗いたら出てました。
http://karlshifflett.wordpress.com/xaml-power-toys/

上記ページのDownloadsという項目のところにインストーラをダウンロードするためのリンクがはられています。
setup.exeをダブルクリックして、ひたすらYesマンになればインストールは完了です。

v4までは、インストール後にオプションのアドイン/マクロ セキュリティにXAML Power Toysのインストールフォルダを追加する必要がありましたが、今回からは必要ないみたいです。

既に追加してる人はさくっと消してしまいましょう。
image 
上記のスクリーンショットで選択している項目がある場合は消す。

軽く使ってみる

ちょっと軽く使って見ます。
「ToysHelloWorld」という名前でWPF アプリケーションを作成します。

Modelsフォルダを作成して、いつも作ってるPersonクラスを、さくっと作ります。

using System;

namespace ToysHelloWorld.Models
{
    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
    }
}

続けて、XAML Power Toysを使うための下準備をします。
XAML Power Toysは、void OnPropertyChanged(string name)という名前のメソッドを持つViewModelBaseというクラスと、RelayCommandというクラスを準備していると、使い勝手がいいように作られています。
まず、ViewModelBaseをさくっと作成します。ViewModelsというフォルダを作成してそこに以下のコードを作成します。

using System.ComponentModel;

namespace ToysHelloWorld.ViewModels
{
    public class ViewModelBase : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged メンバ
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string name)
        {
            var h = PropertyChanged;
            if (h != null)
            {
                h(this, new PropertyChangedEventArgs(name));
            }
        }
        #endregion
    }
}

RelayCommandは、XAML Power Toysのインストーラと一緒に入っているので、それをこぴぺして使います。
ここでは、Commandsというフォルダを作成して、そこにコピペして、namespaceをToysHelloWorld.Commandsに変更しました。

Viewの作成

Viewとして、ユーザコントロールを1つ作成します。
Viewsというフォルダを作成して、PersonViewという名前でユーザコントロールを作成しました。

<UserControl x:Class="ToysHelloWorld.Views.PersonView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <TextBlock Text="PersonView" />
    </Grid>
</UserControl>

とりあえず、表示されればいいので、中身は適当にTextBlockを1つ置いておきます。

ViewModelの作成

続いてViewModelを作っていきます。
ViewModelsフォルダにPersonViewModelという名前のクラスを作成して、ViewModelBaseを継承させます。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ToysHelloWorld.ViewModels
{
    public class PersonViewModel : ViewModelBase
    {

    }
}

ここで一回ビルドして、XAML Power Toysが、アセンブリを認識できるようにしておきます。(超重要)
この10行目あたりにカーソルを持っていって、右クリックメニューからXAML Power Toys → Create ViewModel For Classを選択します。
image

そして、先ほど作ったPersonクラスを選択します。
image

Nextを押すと、下のような画面が出てきます。
image

ここで、プロパティをReadOnlyにするかとか細かく色々設定したり、下のように、プロパティの型を単一の要素から、Listにすることもできます。
image

さらに、Step2のタブに行くと、ViewModelの元になるModelクラス(今回の例ではPersonクラス)のプロパティをラップするような形のプロパティを定義するかを指定できます。
image

さらに、AddCommandボタンを押すと、ViewModelに追加するコマンドも作成できます。
 image

ここでは、Selectという名前のコマンドを、CanExecute無しの状態で作っています。
Createボタンを押すことで、コマンドが追加されます。

最後に、Create ViewModelの画面で、Createボタンを押すと、クリップボードに今作ったViewModelのコードが追加されます。コードを貼り付けると、以下のような感じになります。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ToysHelloWorld.ViewModels
{
    public class PersonViewModel : ViewModelBase
    {

        //TODO developers please add your constructors in the below constructor region.
        //     be sure to include an overloaded constructor that takes a model type.

        #region Declarations

        private ICommand _selectCommand;
        private Person _person;

        #endregion

        #region Properties

        public Person Person
        {
            get { return _person; }
            set
            {
                _person = value;
                OnPropertyChanged("Person");
            }
        }

        public DateTime Birthday
        {
            get { return _person.Birthday; }
            set
            {
                _person.Birthday = value;
                OnPropertyChanged("Birthday");
            }
        }

        public Int32 ID
        {
            get { return _person.ID; }
            set
            {
                _person.ID = value;
                OnPropertyChanged("ID");
            }
        }

        public String Name
        {
            get { return _person.Name; }
            set
            {
                _person.Name = value;
                OnPropertyChanged("Name");
            }
        }


        #endregion

        #region Command Properties

        public ICommand SelectCommand
        {
            get
            {
                if (_selectCommand == null)
                {
                    _selectCommand = new RelayCommand(SelectExecute);
                }
                return _selectCommand;
            }
        }

        #endregion

        #region Constructors

        //TODO developers add your constructors here

        #endregion

        #region Command Methods

        private void SelectExecute(object param)
        {

        }

        #endregion

    }
}

usingがいくつか足りないと怒られるので、さくっと足しておきます。Constructorsのところにある、TODOコメントを消して、以下のようなコンストラクタをさくっと書いておきます。

#region Constructors

public PersonViewModel(Person person)
{
    this.Person = person;
}

#endregion

最後に、Selectコマンドの処理を書いていきます。
ここでは、コマンドが走ったことがわかればいいので、Messageプロパティを新設して、そこに適当な文字列を突っ込むようにしました。

ここら辺は、OnPropertyChangedを呼ぶようなコードスニペットを準備しておくと楽チンです。(過去に紹介したタイプセーフなやり方でもOKです。)

#region Command Methods

private void SelectExecute(object param)
{
    this.Message = string.Format(
        "{0}: {1}, {2:yyyy/MM/dd}",
        this.Person.ID,
        this.Person.Name,
        this.Person.Birthday);
}

#endregion

#region "追加したプロパティ"

private string _message;
public string Message
{
    get { return _message; }
    set
    {
        _message = value;
        OnPropertyChanged("Message");
    }
}
#endregion

ここまで出来たら、ビルドしてコンパイルエラーが出ないことを確認します。(重要!)

Viewの作成 その2

ViewModelが出来たので、先ほど、どんがらだけを作ったViewの作成の続きをやります。
PersonViewを開いて、Gridタグとかを消して、UserControlだけにします。
その状態で、右クリックメニューからXAML Power Toys→Create Form, ListView or DataGrid From Selected Classを選択します。
image

出てきた画面から、先ほど作成したPersonViewModelを選択します。
image

出てきた画面の左側にあるプロパティをドラッグアンドドロップで、画面右側にもっていきます。
 image

各種設定を上記画面のようにして、 Createボタンを押すと、クリップボードにXAMLがコピーされるので、PersonViewに貼り付けます。

<UserControl x:Class="ToysHelloWorld.Views.PersonView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Border BorderBrush="LightGray" BorderThickness="1" CornerRadius="10" Padding="10">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Text="人間様編集フォーム" />
            <Label Grid.Column="0" Grid.Row="1" Content="ID" />
            <Label Grid.Column="0" Grid.Row="2" Content="Name" />
            <Label Grid.Column="0" Grid.Row="3" Content="Birthday" />
            <Label Grid.Column="0" Grid.Row="4" Content="Message" />


            <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Path=ID}" />
            <TextBox Grid.Column="1" Grid.Row="2" Text="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Width="250" HorizontalAlignment="Left" VerticalAlignment="Top" />
            <TextBox Grid.Column="1" Grid.Row="3" Text="{Binding Path=Birthday, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, StringFormat=\{0:yyyy/MM/dd\}}" Width="250" HorizontalAlignment="Left" VerticalAlignment="Top" />
            <TextBlock Grid.Column="1" Grid.Row="4" Text="{Binding Path=Message}" />

            <Grid Grid.Column="0" Grid.Row="5" Grid.ColumnSpan="2" Grid.IsSharedSizeScope="True" HorizontalAlignment="Right">
                <!-- ここら辺を書き換えて Selectコマンドとボタンの紐付けやってます -->
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="Buttons" />
                </Grid.ColumnDefinitions>
                <Button Content="Select" Padding="3.5,0,3.5,0" Margin="3" Command="{Binding SelectCommand}"/>
            </Grid>

        </Grid>
    </Border>

</UserControl>

これで、Viewは出来上がりです。

仕上げ(紐付け)

最後に、ViewModelとViewの紐付けを行います。
App.xamlにDataTemplateを追加して、PersonViewModelとPersonViewを対応付けます。

<Application x:Class="ToysHelloWorld.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:viewModels="clr-namespace:ToysHelloWorld.ViewModels"
    xmlns:views="clr-namespace:ToysHelloWorld.Views"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <DataTemplate DataType="{x:Type viewModels:PersonViewModel}">
            <views:PersonView />
        </DataTemplate>
    </Application.Resources>
</Application>

そして、今まで影の薄かったWindow1のコンストラクタで、ContentにPersonViewModelを設定します。

using System;
using System.Windows;
using ToysHelloWorld.Models;
using ToysHelloWorld.ViewModels;

namespace ToysHelloWorld
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            Content = new PersonViewModel(
                new Person 
                { 
                    ID = 10000, 
                    Name = "田中 麻呂", 
                    Birthday = DateTime.ParseExact("1981/01/30", "yyyy/MM/dd", null)
                });
        }
    }
}

実行してみよう

実行してみると、以下のような画面が表示されます。
image

適当に値を書き換えて、Selectボタンを押すと、ちゃんと動いてることがわかります。
image

簡単にまとめ

ということで、XAML Power Toysを超簡単に使ってみました。
使った機能以外にも、DataGridやListViewに対応したXAMLを出力したりと、使いでのある感じです。

ただ、ちょっと修正が入ると、コードの再生成しなおしっぽいので、そこらへんちょっと大変そうな気がしました。

とりあえずひながたコードをXAML Power Toysでつくって、それをベースに作りこんでいくのがいいのかもしれないです。

投稿日時 : 2009年9月27日 23:15

Feedback

# full welded ball valve 2012/10/18 23:00 http://www.jonloovalve.com/Full-welded-ball-valve-

What i do not realize is in fact how you are not actually a lot more smartly-appreciated than you might be now. You're very intelligent. You know thus significantly on the subject of this topic, made me personally believe it from numerous varied angles. Its like men and women aren't involved except it's one thing to accomplish with Girl gaga! Your personal stuffs outstanding. Always take care of it up!

# ugg sale 2012/10/19 13:00 http://www.superbootonline.com

Regards for helping out, wonderful info. "I have witnessed the softening of the hardest of hearts by a simple smile." by Goldie Hawn.

# women t shirts 2012/10/27 22:07 http://www.burberryoutletonlineshopping.com/burber

You are my inhalation, I own few web logs and very sporadically run out from post :). "Actions lie louder than words." by Carolyn Wells.
women t shirts http://www.burberryoutletonlineshopping.com/burberry-womens-shirts.html

# Burberry Tie 2012/10/27 22:07 http://www.burberryoutletonlineshopping.com/burber

I have not checked in here for a while because I thought it was getting boring, but the last several posts are good quality so I guess I will add you back to my daily bloglist. You deserve it my friend :)
Burberry Tie http://www.burberryoutletonlineshopping.com/burberry-ties.html

# burberry watches for women 2012/10/27 22:07 http://www.burberryoutletonlineshopping.com/burber

F*ckin' awesome issues here. I'm very satisfied to look your article. Thanks a lot and i'm taking a look forward to contact you. Will you kindly drop me a e-mail?
burberry watches for women http://www.burberryoutletonlineshopping.com/burberry-watches.html

# burberry wallets 2012/10/27 22:07 http://www.burberryoutletonlineshopping.com/burber

You are my aspiration, I possess few blogs and sometimes run out from post :). "Analyzing humor is like dissecting a frog. Few people are interested and the frog dies of it." by E. B. White.
burberry wallets http://www.burberryoutletonlineshopping.com/burberry-wallets-2012.html

# burberry mens shirts 2012/10/27 22:07 http://www.burberryoutletonlineshopping.com/burber

I gotta favorite this website it seems handy handy
burberry mens shirts http://www.burberryoutletonlineshopping.com/burberry-men-shirts.html

# burberry bags 2012/10/27 22:07 http://www.burberryoutletonlineshopping.com/burber

Enjoyed studying this, very good stuff, regards . "If it was an overnight success, it was one long, hard, sleepless night." by Dicky Barrett.
burberry bags http://www.burberryoutletonlineshopping.com/burberry-tote-bags.html

# scarf 2012/10/27 22:07 http://www.burberryoutletonlineshopping.com/burber

Good ? I should definitely pronounce, impressed with your web site. I had no trouble navigating through all tabs as well as related info ended up being truly simple to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your client to communicate. Excellent task.
scarf http://www.burberryoutletonlineshopping.com/burberry-scarf.html

# Good day! Do you know if they make any plugins to safeguard against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any recommendations? 2019/04/03 16:24 Good day! Do you know if they make any plugins to

Good day! Do you know if they make any plugins to safeguard against hackers?
I'm kinda paranoid about losing everything I've worked hard on. Any recommendations?

# What a material of un-ambiguity and preserveness of precious knowledge regarding unpredicted feelings. 2019/04/10 9:51 What a material of un-ambiguity and preserveness o

What a material of un-ambiguity and preserveness of
precious knowledge regarding unpredicted feelings.

# I am truly grateful to the owner of this website who has shared this fantastic paragraph at at this time. 2019/04/19 22:08 I am truly grateful to the owner of this website

I am truly grateful to the owner of this website who has shared this
fantastic paragraph at at this time.

# If you want to improve your know-how just keep visiting this web page and be updated with the hottest news update posted here. 2019/07/17 0:17 If you want to improve your know-how just keep vis

If you want to improve your know-how just keep visiting this web page and be updated
with the hottest news update posted here.

# If you want to improve your know-how just keep visiting this web page and be updated with the hottest news update posted here. 2019/07/17 0:18 If you want to improve your know-how just keep vis

If you want to improve your know-how just keep visiting this web page and be updated
with the hottest news update posted here.

# If you want to improve your know-how just keep visiting this web page and be updated with the hottest news update posted here. 2019/07/17 0:19 If you want to improve your know-how just keep vis

If you want to improve your know-how just keep visiting this web page and be updated
with the hottest news update posted here.

# If you want to improve your know-how just keep visiting this web page and be updated with the hottest news update posted here. 2019/07/17 0:20 If you want to improve your know-how just keep vis

If you want to improve your know-how just keep visiting this web page and be updated
with the hottest news update posted here.

# Hi there! This post could not be written any better! Reading through this post reminds me of my old room mate! He always kept talking about this. I will forward this post to him. Pretty sure he will have a good read. Many thanks for sharing! 2019/07/30 23:20 Hi there! This post could not be written any bette

Hi there! This post could not be written any better!
Reading through this post reminds me of my old room
mate! He always kept talking about this. I will forward this
post to him. Pretty sure he will have a good read.
Many thanks for sharing!

# Hi there! This post could not be written any better! Reading through this post reminds me of my old room mate! He always kept talking about this. I will forward this post to him. Pretty sure he will have a good read. Many thanks for sharing! 2019/07/30 23:21 Hi there! This post could not be written any bette

Hi there! This post could not be written any better!
Reading through this post reminds me of my old room
mate! He always kept talking about this. I will forward this
post to him. Pretty sure he will have a good read.
Many thanks for sharing!

# Hi there! This post could not be written any better! Reading through this post reminds me of my old room mate! He always kept talking about this. I will forward this post to him. Pretty sure he will have a good read. Many thanks for sharing! 2019/07/30 23:22 Hi there! This post could not be written any bette

Hi there! This post could not be written any better!
Reading through this post reminds me of my old room
mate! He always kept talking about this. I will forward this
post to him. Pretty sure he will have a good read.
Many thanks for sharing!

# Hi there! This post could not be written any better! Reading through this post reminds me of my old room mate! He always kept talking about this. I will forward this post to him. Pretty sure he will have a good read. Many thanks for sharing! 2019/07/30 23:23 Hi there! This post could not be written any bette

Hi there! This post could not be written any better!
Reading through this post reminds me of my old room
mate! He always kept talking about this. I will forward this
post to him. Pretty sure he will have a good read.
Many thanks for sharing!

# Why people still make use of to read news papers when in this technological globe the whole thing is existing on net? 2019/09/17 8:23 Why people still make use of to read news papers w

Why people still make use of to read news papers when in this technological globe the whole thing is existing on net?

# xupZjnhQPhdmc 2022/04/19 13:38 markus

http://imrdsoacha.gov.co/silvitra-120mg-qrms

タイトル
名前
Url
コメント