かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

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

書庫

日記カテゴリ

[WPF][C#]待望のDataGridコントロール!!お試し

CodePlexにWPFのDataGridコントロールが追加されたとのタレコミ?がBlogのコメントにあった(感謝)
今広島への新幹線内でこの記事を書いているので、ダウンロードのURL等を示すことはできないけど、CodePlexでぐぐって、CodePlex内でWPFあたりで検索すると、WPF関連のページが見つかる。
そこから、WPF Toolkitというのがダウンロードできる。その中にDataGridコントロールが入っている。素敵だ。

早速お試ししてみる。まず、ダウンロードしてきたzipを解凍して、中にあるWPFToolkit.dllをWPFアプリケーションのプロジェクトに参照設定する。

DataGridを使いたいXAMLの適当な場所に名前空間の定義を追加する。

    xmlns:WPFToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"

ここでは、Windowタグの部分に記述した。
これで下準備は完了!おもむろにDataGridをWindowに配置する。この時点で実行しても、当然何も表示されない。
image

ここにデータを表示してみようと思う。表示するデータは、いつもの通りNameとAgeを持つPersonクラスを使ってみる。ということで、いつものコードをさくっと書いた。手が覚えてきたのでかなりのスピードで書けるようになったぜ。

namespace WpfDataTableSample
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

このPersonクラスのリストをDataContextに突っ込んでおく。

namespace WpfDataTableSample
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            DataContext = Enumerable.Range(10, 50).Select(i =>
                new Person { Name = "田中 太郎 no" + i, Age = i % 50 }).ToList();
        }
    }
}

そして、DataGridのItemsSourceにバインドする。

<Window x:Class="WpfDataTableSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WPFToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <WPFToolkit:DataGrid ItemsSource="{Binding}">
        </WPFToolkit:DataGrid>
    </Grid>
</Window>

この状態で実行すると…
image

プロパティ単位で列が作られて、一応編集まで出来ちゃう。なんてこった。罫線も表示されてるし、矢印キーでの上下左右の移動、F2キーで選択中のセルの編集開始といった操作も出来る。いい感じだ。
ただ、日本語で列のヘッダくらいは表示したいってのが人情だと思う。このエントリでは、そこまで書いて終わろうと思う。

今は、自動的に列を定義してくれる機能を使ってるので、プロパティ名がそのまま表示されている。列の定義をきちんとしてあげることで、列のヘッダとかを好きにすることが出来る。

<!-- 自動でカラムを生成しないように指定 -->
<WPFToolkit:DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
    <WPFToolkit:DataGrid.Columns>
        <!-- 名前の表示 -->
        <WPFToolkit:DataGridTextColumn DataFieldBinding="{Binding Name}" 
                                       Header="なまえ" />
        <!-- 年齢の表示 StringFormatでフォーマットも指定してみた -->
        <WPFToolkit:DataGridTextColumn DataFieldBinding="{Binding Age, StringFormat=000歳}"
                                       Header="ねんれい" />
    </WPFToolkit:DataGrid.Columns>
</WPFToolkit:DataGrid>

DataGridのColumnsプロパティが列の定義をするプロパティになる。ここに、必要なだけカラムを定義する。
ざっと見た感じだとCheckBoxやComboBoxみたいなものを表示するカラムもあったりした。ここでは、一番単純そうなDataGridTextColumnを使ってみた。

DataFieldBindingにBindingを渡して、Headerプロパティにヘッダーに表示した内容を書くだけと直感的に使えるようになっている。現に今、このエントリを書きながら一度もマニュアル類を読んでない。にも関わらず、ここらへんまではすんなり進むことができた。
いい感じだ。

さて、実行すると下のように表示される。ヘッダーも日本語になったし、年齢がフォーマッティングされて出力されているのがわかる。
image

今回はここまで。

投稿日時 : 2008年8月15日 18:00

Feedback

# [C#][WPF]WPF ToolkitのDataGridのバグ? 2008/11/07 12:59 かずきのBlog

[C#][WPF]WPF ToolkitのDataGridのバグ?

# Canada Goose Outlet 2012/10/19 14:29 http://www.supercoatsale.com

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

# louis vuitton handbags 2012/10/28 3:13 http://www.louisvuittonoutletbags2013.com/

Acquaintance often is the golden thread who brings together a new minds of all arena.
louis vuitton handbags http://www.louisvuittonoutletbags2013.com/

# bague cartier occasion copie 2017/12/23 9:41 dercrfsruefspkmtmj@hotmal.com

Oui Helios mais jusqu’à maintenant ce sont les seules possibilités pour les agences de pub de gérer en direct live leurs campagnes avec ciblage et fiabilité et aussi avoir un retour sur l’efficacité (per view, per click) car on sait depuis longtemps que les données type OJD pour le papier sont bidonnées… Bon, pour la pub internet il semble qu’il y a aussi des robots cliqueurs…
bague cartier occasion copie http://www.bijouxpopulaire.com/tag/bague-love-cartier-faux

# copy black van cleef necklace 2017/12/23 19:29 deyttzpmxefsbukebtq@hotmal.com

Queria saber uma frase na língua dos elfos “ariel para sempre” ficaria extremamente grata ^^

# replique cartier bracelet love or blanc 2017/12/23 23:47 dernzopelkqefsubtbeuzxq@hotmal.com

Beautifully done. Love the details and that floor is absolutely gorgeous.

# CBcFaUHifvOgrxyACaG 2018/12/20 4:19 https://www.suba.me/

JOmXcM Some genuinely superb content on this site, regards for contribution.

# That is a great tip especially to those fresh to the blogosphere. Short but very precise info… Many thanks for sharing this one. A must read article! 2019/04/02 15:10 That is a great tip especially to those fresh to t

That is a great tip especially to those fresh to the blogosphere.

Short but very precise info… Many thanks for sharing this one.

A must read article!

# An outstanding share! I've just forwarded this onto a coworker who had been conducting a little homework on this. And he actually bought me lunch simply because I stumbled upon it for him... lol. So let me reword this.... Thanks for the meal!! But yeah, 2019/05/09 18:25 An outstanding share! I've just forwarded this ont

An outstanding share! I've just forwarded this onto a coworker
who had been conducting a little homework on this. And he actually bought me lunch simply because I stumbled upon it for
him... lol. So let me reword this.... Thanks for the meal!!
But yeah, thanx for spending some time to discuss this issue
here on your web site.

# You really make it seem so easy with your presentation but I find this matter to be actually something that I think I would never understand. It seems too complex and extremely broad for me. I am looking forward for your next post, I will try to get the 2019/05/16 14:03 You really make it seem so easy with your presenta

You really make it seem so easy with your presentation but I find this matter
to be actually something that I think I would never understand.
It seems too complex and extremely broad for me. I am looking forward for your
next post, I will try to get the hang of it!

# This is a good tip especially to those new to the blogosphere. Simple but very accurate information… Thanks for sharing this one. A must read post! 2019/06/02 2:39 This is a good tip especially to those new to the

This is a good tip especially to those new to the blogosphere.
Simple but very accurate information… Thanks for sharing
this one. A must read post!

# Hello There. I discovered your weblog the use of msn. This is a very well written article. I'll make sure to bookmark it and return to read extra of your helpful info. Thanks for the post. I will certainly comeback. 2019/06/05 0:26 Hello There. I discovered your weblog the use of m

Hello There. I discovered your weblog the
use of msn. This is a very well written article. I'll make sure to bookmark it and return to read extra of your helpful info.
Thanks for the post. I will certainly comeback.

# Awesome! Its actually remarkable article, I have got much clear idea about from this piece of writing. 2019/06/05 21:41 Awesome! Its actually remarkable article, I have g

Awesome! Its actually remarkable article, I have got much clear idea about
from this piece of writing.

# Fabulous, what a blog it is! This webpage provides useful information to us, keep it up. 2021/09/03 4:36 Fabulous, what a blog it is! This webpage provides

Fabulous, what a blog it is! This webpage provides useful information to us, keep it up.

# Hello, just wanted to tell you, I enjoyed this post. It was inspiring. Keep on posting! 2021/09/05 10:27 Hello, just wanted to tell you, I enjoyed this pos

Hello, just wanted to tell you, I enjoyed this post.
It was inspiring. Keep on posting!

# Why users still make use of to read news papers when in this technological world all is existing on net? quest bars http://bitly.com/3C2tkMR quest bars 2021/09/10 9:53 Why users still make use of to read news papers wh

Why users still make use of to read news papers when in this
technological world all is existing on net? quest bars http://bitly.com/3C2tkMR quest bars

# prednisone 12 tablets price https://prednisonesnw.com/#
generic prednisone online 2021/11/13 9:45 Prednisone

prednisone 12 tablets price https://prednisonesnw.com/#
generic prednisone online

# barinat https://baricitinibrx.com
covid treatment baricitinib
2021/12/12 16:32 Buksdldj

barinat https://baricitinibrx.com
covid treatment baricitinib

# careprost for sale https://bimatoprostrx.com
careprost bimatoprost for sale
2021/12/13 16:14 Hksfnjkh

careprost for sale https://bimatoprostrx.com
careprost bimatoprost for sale

# buy cheap doxycycline https://doxycyline1st.com/
buy cheap doxycycline online 2022/02/26 8:26 Jusidkid

buy cheap doxycycline https://doxycyline1st.com/
buy cheap doxycycline online

# Heya i'm for the first time here. I found this board and I find It really useful & it helped me out much. I hope to give something back and help others like you aided me. 2022/03/24 8:34 Heya i'm for the first time here. I found this boa

Heya i'm for the first time here. I found this board and I find It really useful
& it helped me out much. I hope to give something back and help others like
you aided me.

# Heya i'm for the first time here. I found this board and I find It really useful & it helped me out much. I hope to give something back and help others like you aided me. 2022/03/24 8:36 Heya i'm for the first time here. I found this boa

Heya i'm for the first time here. I found this board and I find It really useful
& it helped me out much. I hope to give something back and help others like
you aided me.

# Heya i'm for the first time here. I found this board and I find It really useful & it helped me out much. I hope to give something back and help others like you aided me. 2022/03/24 8:37 Heya i'm for the first time here. I found this boa

Heya i'm for the first time here. I found this board and I find It really useful
& it helped me out much. I hope to give something back and help others like
you aided me.

# Heya i'm for the first time here. I found this board and I find It really useful & it helped me out much. I hope to give something back and help others like you aided me. 2022/03/24 8:38 Heya i'm for the first time here. I found this boa

Heya i'm for the first time here. I found this board and I find It really useful
& it helped me out much. I hope to give something back and help others like
you aided me.

# Hi there! I know this is somewhat off topic but I was wondering which blog platform are you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform. I would be awes 2022/03/25 8:10 Hi there! I know this is somewhat off topic but I

Hi there! I know this is somewhat off topic but I was wondering which blog platform are you using for this
site? I'm getting sick and tired of Wordpress because
I've had issues with hackers and I'm looking at options for another platform.
I would be awesome if you could point me in the direction of a good platform.

# Hi there! I know this is somewhat off topic but I was wondering which blog platform are you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform. I would be awes 2022/03/25 8:11 Hi there! I know this is somewhat off topic but I

Hi there! I know this is somewhat off topic but I was wondering which blog platform are you using for this
site? I'm getting sick and tired of Wordpress because
I've had issues with hackers and I'm looking at options for another platform.
I would be awesome if you could point me in the direction of a good platform.

# Hi there! I know this is somewhat off topic but I was wondering which blog platform are you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform. I would be awes 2022/03/25 8:12 Hi there! I know this is somewhat off topic but I

Hi there! I know this is somewhat off topic but I was wondering which blog platform are you using for this
site? I'm getting sick and tired of Wordpress because
I've had issues with hackers and I'm looking at options for another platform.
I would be awesome if you could point me in the direction of a good platform.

# Hi there! I know this is somewhat off topic but I was wondering which blog platform are you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform. I would be awes 2022/03/25 8:14 Hi there! I know this is somewhat off topic but I

Hi there! I know this is somewhat off topic but I was wondering which blog platform are you using for this
site? I'm getting sick and tired of Wordpress because
I've had issues with hackers and I'm looking at options for another platform.
I would be awesome if you could point me in the direction of a good platform.

# If you wish for to grow your experience simply keep visiting this site and be updated with the hottest information posted here. http://wiki.xn--80ahlrjqm6azc.xn--p1ai/index.php/A_Third_Possibility_Is_A_Mortgage_Broker https://prowrestling.wiki/index.p 2022/05/31 16:07 If you wish for to grow your experience simply kee

If you wish for to grow your experience simply keep visiting this site and be updated with the hottest information posted here.



http://wiki.xn--80ahlrjqm6azc.xn--p1ai/index.php/A_Third_Possibility_Is_A_Mortgage_Broker
https://prowrestling.wiki/index.php?title=User:BrandonBriscoe5
https://wiki.warofmyths.com/index.php/Seven_Methods_To_Reinvent_Your_Mortgage_Broker
http://datasciencemetabase.com/index.php/What_Is_The_Wage_Of_A_Mortgage_Broker
http://skc.isi.edu/workflow-design/index.php/Mortgage_Lenders_In_Riverside_Ca_Providers_-_Tips_On_How_To_Do_It_Right
https://portpavement.com/index.php/Mastering_The_Way_In_Which_Of_Mortgage_Broker_Is_Just_Not_An_Accident_-_It_s_An_Artwork

# doors2.txt;1 2023/03/14 15:34 jOWhvwImEstoTDsND

doors2.txt;1

# doors2.txt;1 2023/03/14 17:00 iVgAEqkngSHrBMz

doors2.txt;1

# canada rx https://pillswithoutprescription.pro/# 2023/05/15 5:19 PillsPresc

canada rx https://pillswithoutprescription.pro/#

# online meds https://pillswithoutprescription.pro/# 2023/05/16 11:34 PillsPro

online meds https://pillswithoutprescription.pro/#

# prednisone 10mg canada https://prednisone.bid/ prednisone 20 mg tablet price 2023/12/27 11:31 Prednisone

prednisone 10mg canada https://prednisone.bid/ prednisone 20 mg tablet price

# prednisone 2 mg https://prednisonepharm.store/ buy prednisone with paypal canada 2024/01/20 18:05 Prednisone

prednisone 2 mg https://prednisonepharm.store/ buy prednisone with paypal canada

# gates of olympus oyna &#252;cretsiz - https://gatesofolympus.auction/ gates of olympus oyna demo 2024/03/27 21:12 Olympic

gates of olympus oyna &#252;cretsiz - https://gatesofolympus.auction/ gates of olympus oyna demo

タイトル
名前
Url
コメント