かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

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

書庫

日記カテゴリ

[C#][WPF]あるクラスを作ってみた

ネタ元:【WPF】ViewからViewModelへViewのインスタンスを渡す(CommandParameter経由編)
と、ここでハタと思い付いた。あるクラスを用意し、その依存関係プロパティにViewのインスタンスを突っ込み、そのクラスでViewModelにそのインスタンスを渡してやればいいんじゃないかと。そのクラスはちょうどViewからViewModelへViewのインスタンスを橋渡しする働きをするわけだ。
明日にでも試してみよう。今日はこれから飲み会なんで。

飲み会の隙を伺って書いてみた。

using System.Windows;

namespace WpfModelViewApplication1.ViewModels
{
    public static class あるクラス
    {
        #region ViewModelを保持するプロパティ
        public static object GetViewModel(DependencyObject obj)
        {
            return (object)obj.GetValue(ViewModelProperty);
        }

        public static void SetViewModel(DependencyObject obj, object value)
        {
            obj.SetValue(ViewModelProperty, value);
        }

        public static readonly DependencyProperty ViewModelProperty =
            DependencyProperty.RegisterAttached("ViewModel", typeof(object), typeof(あるクラス), new UIPropertyMetadata(null, PropertyChanged));
        #endregion

        #region ViewModelのViewをセットするプロパティ名
        public static string GetPropertyPath(DependencyObject obj)
        {
            return (string)obj.GetValue(PropertyPathProperty);
        }

        public static void SetPropertyPath(DependencyObject obj, string value)
        {
            obj.SetValue(PropertyPathProperty, value);
        }

        public static readonly DependencyProperty PropertyPathProperty =
            DependencyProperty.RegisterAttached("PropertyPath", typeof(string), typeof(あるクラス), new UIPropertyMetadata(null, PropertyChanged));
        #endregion

        #region プロパティ変更時の処理
        private static void PropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            // viewはsenderのはず
            object view = sender;
            // viewmodelと
            object viewModel = GetViewModel(sender);
            // プロパティ名をとって
            string property = GetPropertyPath(sender);
            if (view == null || viewModel == null || property == null)
            {
                // 3つ設定されてなかったら何もしない
                return;
            }

            // viewmodelにviewを設定する
            var prop = viewModel.GetType().GetProperty(property);
            prop.SetValue(viewModel, view, null);
        }
        #endregion
    }
}

もうちょっとイケテル感じには出来るけど、お昼休みの限られた時間でということなので暫定版!使い方は以下の通り。

ViewModelにViewを受け入れるプロパティを定義!(IMainViewがViewが実装するインターフェース)

public class MainViewModel : ViewModelBase
{
    // Viewを設定するプロパティ
    public IMainView MainView { get; set; }
}

そして、XAML側に以下のような定義を足す。

<Window x:Class="WpfModelViewApplication1.Views.MainView"
    ...略...
    xmlns:local="clr-namespace:WpfModelViewApplication1.ViewModels"
    local:あるクラス.ViewModel="{Binding}"
    local:あるクラス.PropertyPath="MainView"
    Title="Main Window" Height="400" Width="800">
...略...
</Window>

これで完成。
後は、ViewModel側の適当なメソッドでViewを自由自在に弄れます。

 

# あとでやるかもしれない改良案
# ViewModelとProperty名を保持するクラスを作って、そいつを添付プロパティとして設定するといいかもしれない。
# <あるクラス.ViewModelMapping>
#   <ViewModelとProperty名を保持するクラス ViewModel=”{Binding}” Property名=”MainView” />
# <あるクラス.ViewModelMapping>
# 後で

投稿日時 : 2009年8月11日 12:57

Feedback

# louis vuitton handbags 2012/10/28 2:48 http://www.louisvuittonoutletbags2013.com/

One particular sibling are not somebody, yet somebody have been a good solid sibling.
louis vuitton handbags http://www.louisvuittonoutletbags2013.com/

# womens shirts 2012/10/28 13:26 http://www.burberryoutletscarfsale.com/burberry-wo

Simply wanna remark on few general things, The website pattern is perfect, the content is rattling great : D.
womens shirts http://www.burberryoutletscarfsale.com/burberry-womens-shirts.html

# burberry sale 2012/11/01 8:57 http://www.burberryoutletlocations.com

I do believe all the concepts you've introduced for your post. They're very convincing and will certainly work. Nonetheless, the posts are too brief for beginners. May just you please extend them a little from next time? Thanks for the post.
burberry sale http://www.burberryoutletlocations.com

# I am no longer positive the place you are getting your information, but great topic. I needs to spend a while studying much more or figuring out more. Thanks for great information I was on the lookout for this info for my mission. 2018/10/06 2:48 I am no longer positive the place you are getting

I am no longer positive the place you are getting your information, but great topic.
I needs to spend a while studying much more or figuring out
more. Thanks for great information I was on the lookout for
this info for my mission.

# Hi there it's me, I am also visiting this website on a regular basis, this website is in fact pleasant and the users are genuinely sharing pleasant thoughts. 2018/10/18 3:18 Hi there it's me, I am also visiting this website

Hi there it's me, I am also visiting this website on a regular basis, this website is in fact pleasant and the users
are genuinely sharing pleasant thoughts.

# Howdy! I could have sworn I've been to this web site before but after browsing through a few of the posts I realized it's new to me. Nonetheless, I'm definitely delighted I found it and I'll be book-marking it and checking back frequently! 2018/10/20 9:13 Howdy! I could have sworn I've been to this web s

Howdy! I could have sworn I've been to this web site before but after browsing through a
few of the posts I realized it's new to me.
Nonetheless, I'm definitely delighted I found it and I'll be book-marking it and checking back frequently!

# I for all time emailed this blog post page to all my friends, because if like to read it afterward my contacts will too. 2018/11/20 23:40 I for all time emailed this blog post page to all

I for all time emailed this blog post page to all my friends, because
if like to read it afterward my contacts will too.

# re: [C#][WPF]??????????? 2021/08/08 6:42 hydroxychlor 200mg

is chloroquine over the counter https://chloroquineorigin.com/# quinoline sulfate

# Hi there just wanted to give you a quick heads up and let you know a few of the images aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different internet browsers and both show the same results. 2021/09/17 10:27 Hi there just wanted to give you a quick heads up

Hi there just wanted to give you a quick heads up and let you know a few
of the images aren't loading correctly. I'm not sure why but I think its a linking issue.

I've tried it in two different internet browsers and both
show the same results.

# Good day! I could have sworn I've been to this website before but after checking through some of the post I realized it's new to me. Anyways, I'm definitely glad I found it and I'll be book-marking and checking back often! 2021/12/16 20:04 Good day! I could have sworn I've been to this web

Good day! I could have sworn I've been to this website before but after checking through
some of the post I realized it's new to me.
Anyways, I'm definitely glad I found it and I'll be book-marking and
checking back often!

# Marvelous, what a weblog it is! This blog gives valuable data to us, keep it up. 2022/02/06 6:33 Marvelous, what a weblog it is! This blog gives va

Marvelous, what a weblog it is! This blog gives valuable data to us, keep it up.

# Howdy just wanted to give you a quick heads up. The text in your content seem to be running off the screen in Ie. I'm not sure if this is a formatting issue or something to do with web browser compatibility but I figured I'd post to let you know. The de 2022/03/23 5:03 Howdy just wanted to give you a quick heads up. T

Howdy just wanted to give you a quick heads up. The text in your content seem to be running off
the screen in Ie. I'm not sure if this is a formatting issue or something to do with web
browser compatibility but I figured I'd post to let you know.
The design look great though! Hope you get the problem fixed soon. Kudos

# Excellent blog you've got here.. It's difficult to find good quality writing like yours these days. I truly appreciate people like you! Take care!! 2022/03/24 18:49 Excellent blog you've got here.. It's difficult t

Excellent blog you've got here.. It's difficult to find good quality writing like yours these days.

I truly appreciate people like you! Take care!!

# Excellent blog you've got here.. It's difficult to find good quality writing like yours these days. I truly appreciate people like you! Take care!! 2022/03/24 18:50 Excellent blog you've got here.. It's difficult t

Excellent blog you've got here.. It's difficult to find good quality writing like yours these days.

I truly appreciate people like you! Take care!!

# Excellent blog you've got here.. It's difficult to find good quality writing like yours these days. I truly appreciate people like you! Take care!! 2022/03/24 18:50 Excellent blog you've got here.. It's difficult t

Excellent blog you've got here.. It's difficult to find good quality writing like yours these days.

I truly appreciate people like you! Take care!!

# Excellent blog you've got here.. It's difficult to find good quality writing like yours these days. I truly appreciate people like you! Take care!! 2022/03/24 18:51 Excellent blog you've got here.. It's difficult t

Excellent blog you've got here.. It's difficult to find good quality writing like yours these days.

I truly appreciate people like you! Take care!!

# Lots of people have been playing these games, but to numerous, winning the overall game is really a problem. Lots of people consider themselves unlucky if they don't win the game, but luck isn't the case. There are many easy processes for winning the lot 2022/03/28 21:39 Lots of people have been playing these games, but

Lots of people have been playing these games, but to numerous, winning the overall game is really a problem.
Lots of people consider themselves unlucky if they don't
win the game, but luck isn't the case. There are many easy processes for winning the lottery game.
First you have to be patient and persevere with situations if you don't win.
Failing the very first time you bet does not mean that you're unlucky and cannot win the
game. If you lose the game, just keep on trying and 1 day
you will win. Playing the game many times also earns you feel and familiarizes you with the principles of the
game.The other easy method for winning the lottery game is always to take advantage of
the lottery software. This software automatically chooses number combination that
have high chances of winning. Additionally, it monitors previous
draw results from where you can compare and make number combination's that win. Another technique is
always to play the lottery with lower jackpots and fewer
players. This will raise your chances for winning the jackpot.
Higher jackpot means more competition for winning number and less likelihood
of winning the lottery game. As an example, play five ball
games which have the fewest numbers possible.
Ideally games with as few as thirty five numbers are good, however all of the times
they have few prizes. You can balance the limited amount of prize
against what you may want in the game.

# Spot on with this write-up, I truly believe this amazing site needs far more attention. I'll probably be returning to read through more, thanks for the information! 2022/04/12 18:29 Spot on with this write-up, I truly believe this a

Spot on with this write-up, I truly believe this amazing
site needs far more attention. I'll probably be returning to
read through more, thanks for the information!

# Incredible points. Outstanding arguments. Keep up the great effort. 2022/04/28 2:24 Incredible points. Outstanding arguments. Keep up

Incredible points. Outstanding arguments. Keep up the great effort.

# Its such as you learn my thoughts! You seem to grasp so much approximately this, such as you wrote the ebook in it or something. I believe that you simply can do with a few percent to pressure the message home a little bit, but other than that, that is 2022/04/28 22:49 Its such as you learn my thoughts! You seem to gra

Its such as you learn my thoughts! You seem to grasp so much approximately this,
such as you wrote the ebook in it or something. I believe that
you simply can do with a few percent to pressure the
message home a little bit, but other than that,
that is wonderful blog. An excellent read. I
will definitely be back.

# I was recommended this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my trouble. You're amazing! Thanks! 2022/05/03 21:02 I was recommended this web site by my cousin. I am

I was recommended this web site by my cousin. I am not sure whether this post is written by him as
nobody else know such detailed about my trouble. You're amazing!
Thanks!

# Hi! Do you know if they make any plugins to help with Search Engine Optimization? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good success. If you know of any please share. Thanks! 2022/05/05 15:17 Hi! Do you know if they make any plugins to help w

Hi! Do you know if they make any plugins to help
with Search Engine Optimization? I'm trying
to get my blog to rank for some targeted keywords but I'm not seeing very good success.
If you know of any please share. Thanks!

# Hi there! I simply would like to offer you a big thumbs up for your excellent information you've got here on this post. I'll be coming back to your website for more soon. 2022/05/25 11:30 Hi there! I simply would like to offer you a big t

Hi there! I simply would like to offer you a big thumbs up for your excellent information you've
got here on this post. I'll be coming back to your website for more soon.

# Hi there! I simply would like to offer you a big thumbs up for your excellent information you've got here on this post. I'll be coming back to your website for more soon. 2022/05/25 11:30 Hi there! I simply would like to offer you a big t

Hi there! I simply would like to offer you a big thumbs up for your excellent information you've
got here on this post. I'll be coming back to your website for more soon.

# Hi there! I simply would like to offer you a big thumbs up for your excellent information you've got here on this post. I'll be coming back to your website for more soon. 2022/05/25 11:31 Hi there! I simply would like to offer you a big t

Hi there! I simply would like to offer you a big thumbs up for your excellent information you've
got here on this post. I'll be coming back to your website for more soon.

# Hi there! I simply would like to offer you a big thumbs up for your excellent information you've got here on this post. I'll be coming back to your website for more soon. 2022/05/25 11:31 Hi there! I simply would like to offer you a big t

Hi there! I simply would like to offer you a big thumbs up for your excellent information you've
got here on this post. I'll be coming back to your website for more soon.

# Hi, I do think this is a great blog. I stumbledupon it ;) I'm going to come back yet again since i have book marked it. Money and freedom is the greatest way to change, may you be rich and continue to help others. 2022/06/02 8:43 Hi, I do think this is a great blog. I stumbledupo

Hi, I do think this is a great blog. I stumbledupon it ;) I'm going to come back yet again since i have book marked it.
Money and freedom is the greatest way to change, may you be rich and continue to help others.

# It's hard to come by experienced people in this particular topic, but you seem like you know what you're talking about! Thanks 2022/06/05 8:25 It's hard to come by experienced people in this pa

It's hard to come by experienced people in this particular topic, but you seem like
you know what you're talking about! Thanks

# Hi there! This is kind of off topic but I need some advice from an established blog. Is it very hard to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking about setting up my own but I'm not sure where to s 2022/06/12 21:26 Hi there! This is kind of off topic but I need som

Hi there! This is kind of off topic but I need some advice
from an established blog. Is it very hard to set up your own blog?

I'm not very techincal but I can figure things out
pretty fast. I'm thinking about setting up my own but I'm not sure where to start.

Do you have any points or suggestions? With thanks

# Hello friends, its fantastic paragraph concerning cultureand entirely defined, keep it up all the time. 2022/06/28 0:45 Hello friends, its fantastic paragraph concerning

Hello friends, its fantastic paragraph concerning cultureand entirely defined, keep it up all the time.

# If some one desires to be updated with most up-to-date technologies after that he must be pay a visit this website and be up to date daily. 2022/07/01 15:04 If some one desires to be updated with most up-to-

If some one desires to be updated with most up-to-date technologies after that he must be pay
a visit this website and be up to date daily.

# It's really very complicated in this active life to listen news on TV, thus I just use the web for that purpose, and obtain the most recent information. 2022/07/08 9:39 It's really very complicated in this active life t

It's really very complicated in this active life to listen news
on TV, thus I just use the web for that
purpose, and obtain the most recent information.

# It's truly very complicated in this active life to listen news on Television, so I only use the web for that purpose, and get the latest information. 2022/08/11 14:24 It's truly very complicated in this active life to

It's truly very complicated in this active life to listen news on Television, so I only use the web
for that purpose, and get the latest information.

# I'm not sure exactly why but this blog is loading extremely slow for me. Is anyone else having this issue or is it a problem on my end? I'll check back later and see if the problem still exists. 2022/08/13 5:59 I'm not sure exactly why but this blog is loading

I'm not sure exactly why but this blog is loading extremely slow for me.
Is anyone else having this issue or is it a problem on my
end? I'll check back later and see if the problem still exists.

# Hi there! This is my first comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading through your posts. Can you suggest any other blogs/websites/forums that go over the same subjects? Appreciate it! 2022/08/22 14:19 Hi there! This is my first comment here so I just

Hi there! This is my first comment here so I just wanted to give a quick shout out and tell you
I truly enjoy reading through your posts. Can you suggest
any other blogs/websites/forums that go over
the same subjects? Appreciate it!

# Hi, everything is going fine here and ofcourse every one is sharing data, that's actually good, keep up writing. 2022/08/23 0:42 Hi, everything is going fine here and ofcourse eve

Hi, everything is going fine here and ofcourse every one is sharing data, that's actually good, keep up
writing.

# Thanks for another wonderful article. The place else may anyone get that kind of info in such an ideal way of writing? I have a presentation subsequent week, and I am on the look for such information. 2022/08/26 17:09 Thanks for another wonderful article. The place e

Thanks for another wonderful article. The place else may anyone get that kind of info in such an ideal way of writing?
I have a presentation subsequent week, and I am on the look for such information.

# Good response in return of this difficulty with genuine arguments and explaining everything concerning that. 2022/08/30 21:14 Good response in return of this difficulty with g

Good response in return of this difficulty with genuine arguments and explaining everything concerning that.

# Heya just wanted to give you a quick heads up and let you know a few of the pictures aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different web browsers and both show the same results. 2022/09/04 13:47 Heya just wanted to give you a quick heads up and

Heya just wanted to give you a quick heads up and let you
know a few of the pictures aren't loading correctly. I'm not sure why but I think
its a linking issue. I've tried it in two different web
browsers and both show the same results.

# Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a bit, but instead of that, this is excellent blog. A fantastic read. I will c 2023/05/08 18:51 Its like you read my mind! You appear to know so m

Its like you read my mind! You appear to know so much about this, like you wrote the
book in it or something. I think that you can do with some pics to drive the message home a bit,
but instead of that, this is excellent blog.
A fantastic read. I will certainly be back.

# You made some decent points there. I looked on the internet for additional information about the issue and found most people will go along with your views on this website. 2023/06/30 19:34 You made some decent points there. I looked on the

You made some decent points there. I looked on the internet
for additional information about the issue and found most people will go along with your views on this website.

# I'm not sure where you're getting your info, but great topic. I needs to spend some time learning more or understanding more. Thanks for magnificent info I was looking for this info for my mission. 2023/11/24 4:36 I'm not sure where you're getting your info, but g

I'm not sure where you're getting your info, but great topic.
I needs to spend some time learning more or understanding more.
Thanks for magnificent info I was looking for this info for my mission.

# Excellent article. I'm dealing with some of these issues as well.. 2024/01/14 6:38 Excellent article. I'm dealing with some of these

Excellent article. I'm dealing with some of these
issues as well..

# Hello, i think that i saw you visited my web site so i got here to go back the favor?.I'm trying to to find issues to improve my web site!I assume its ok to make use of some of your ideas!! 2024/02/07 15:55 Hello, i think that i saw you visited my web site

Hello, i think that i saw you visited my web site so
i got here to go back the favor?.I'm trying to to
find issues to improve my web site!I assume its ok to make use of some of your ideas!!

# This site certainly has all the information I wanted about this subject and didn't know who to ask. You can see similar: ecommerce and here sklep online 2024/02/15 8:20 This site certainly has all the information I want

This site certainly has all the information I wanted about this subject and didn't know
who to ask. You can see similar: ecommerce and here sklep online

# Hi! Do you know if they make any plugins to assist with SEO? I'm trying to get my site to rank for some targeted keywords but I'm not seeing very good gains. If you know of any please share. Appreciate it! I saw similar blog here: Backlink Portfolio 2024/04/04 2:15 Hi! Do you know if they make any plugins to assist

Hi! Do you know if they make any plugins to assist with SEO?
I'm trying to get my site to rank for some targeted keywords but I'm not seeing very good gains.
If you know of any please share. Appreciate it! I saw similar blog here: Backlink Portfolio

# Wow, amazing blog structure! How lengthy have you ever been blogging for? you make blogging glance easy. The overall look of your website is magnificent, let alone the content! You can see similar here prev next and it's was wrote by Sophie75. 2024/04/23 3:50 Wow, amazing blog structure! How lengthy have you

Wow, amazing blog structure! How lengthy have you ever been blogging for?
you make blogging glance easy. The overall look of your website is
magnificent, let alone the content! You can see similar here prev
next and it's was wrote by Sophie75.

タイトル
名前
Url
コメント