The road to C# master trapemiya

C#を中心に、.NETの話題を取り上げます。

ホーム 連絡をする 同期する ( RSS 2.0 ) Login
投稿数  256  : 記事  1  : コメント  10531  : トラックバック  30

ニュース

Since 2005年10月26日

わんくま同盟

わんくま同盟

Microsoft MVP


Visual Developer - Visual C#

記事カテゴリ

書庫

日記カテゴリ

前提として、Window1のGrid内に、UserControl1とUserControl2があるものとします。この時、UserControl1からUserControl2の参照を得るにはどのようにすれば良いでしょうか? いくつかのパターンを以下にします。

//パターン1
Window1 mw = (Window1)((Grid)this.Parent).Parent;

//パターン2
Window1 mw = (Window1)Application.Current.MainWindow;

//パターン3
var window = Application.Current.Windows.OfType<Window>().FirstOrDefault(w => w is Window1);
Window1 mw = (Window1)window;

/*以下ではWindow1のNameプロパティにWindow1Nameを設定する必要がある。
 * Window1というクラスと同じ名前は設定できない。
 * これはNameプロパティと同じ名前のフィールドがそのクラスに自動的に作成されるためである。
 */

//パターン4
var window = from w in Application.Current.Windows.OfType<Window>()
                        where w.Name == "Window1Name"
                        select w;
Window1 mw = (Window1)window.FirstOrDefault<Window>();

//パターン5
var window = from Window1 w in Application.Current.Windows.OfType<Window>()
             where w.Name == "Window1Name"
             select w;
Window1 mw = window.FirstOrDefault<Window1>();

こうやってWindow1の参照であるmwが得られたので、あとはmw.UserControl2とすれば、UserControl2の参照が得られます。

投稿日時 : 2009年3月13日 15:32

コメント

# re: 【WPF】Windowに配置されたユーザーコントロールから他のユーザーコントロールの参照を得る方法 2009/03/13 15:36 trapemiya
パターン3からパターン5はLINQの書き方だけの違いなので、本質は3パターンですね。

# re: 【WPF】Windowに配置されたユーザーコントロールから他のユーザーコントロールの参照を得る方法 2009/03/13 16:00 囚人
いまいち問題の本質が理解してませんけど、
Window.GetWindow()
てのもあります。
あとは、VisualTreeHelperクラスとかLogicalTreeHelperクラスを使って、その辺の親子関係ごちゃごちゃできますね。

# re: 【WPF】Windowに配置されたユーザーコントロールから他のユーザーコントロールの参照を得る方法 2009/03/13 16:01 囚人
>いまいち問題の本質が理解してませんけど

いまいち問題の本質を僕が理解してませんけど

# re: 【WPF】Windowに配置されたユーザーコントロールから他のユーザーコントロールの参照を得る方法 2009/03/13 16:01 trapemiya
パターン2はWindow1がメインウインドウである必要があります。

# re: 【WPF】Windowに配置されたユーザーコントロールから他のユーザーコントロールの参照を得る方法 2009/03/13 16:11 trapemiya
>Window.GetWindow()
てのもあります。 CoinMainWindow mw = (CoinMainWindow)

お~、そんなものがあったのか~。探しきれなかった。orz
Window1 mw = (Window1)Window.GetWindow(this);
簡単ですね。
ありがとうございます。

>あとは、VisualTreeHelperクラスとかLogicalTreeHelperクラスを使って、その辺の親子関係ごちゃごちゃできますね。

なるほど。これはちょっと宿題にさせて下さい。

# re: 【WPF】Windowに配置されたユーザーコントロールから他のユーザーコントロールの参照を得る方法 2009/03/13 21:48 えムナウ
>あとは、VisualTreeHelperクラスとかLogicalTreeHelperクラスを使って、その辺の親子関係ごちゃごちゃできますね。
こんな使い方できます。親にもたどれるけどね。
http://blogs.wankuma.com/mnow/archive/2007/10/23/103389.aspx

# re: 【WPF】Windowに配置されたユーザーコントロールから他のユーザーコントロールの参照を得る方法 2009/03/13 22:43 えムナウ
ちなみにTabControlの下にあるUserControlはogicalTreeHelperクラスじゃないとたどれない。

# re: 【WPF】Windowに配置されたユーザーコントロールから他のユーザーコントロールの参照を得る方法 2009/03/14 17:25 trapemiya
>こんな使い方できます。親にもたどれるけどね。

ありがとうございます。今時間が取れないので、あとで見させていただきます。ようやく形のあるアプリケーションをWPFで作ろうとしていまして、苦労の連続です・・・

# re: 【WPF】Windowに配置されたユーザーコントロールから他のユーザーコントロールの参照を得る方法 2009/03/16 0:46 trapemiya
LogicalTreeHelperを使って、以下のように辿れました。ありがとうございます。

Func<DependencyObject, DependencyObject> func = null;
func = (o) => LogicalTreeHelper.GetParent(o) != null ? func(LogicalTreeHelper.GetParent(o)) : o;
Windows1 mw = (Window1)func(this);

ところでユーザーコントロール内部で、それが置かれているウインドウの型に思いっきりキャストしていますが、このウインドウ専用のユーザーコントロールなので許して下さい。本来はイベントで連絡すべきですが、軽いプロトタイプの開発なものですから、許して下さい。


# gagaコピー 2018/02/02 13:41 zldulk@live.jp
ルイヴィトン - N級バッグ、財布 専門サイト問屋
弊社は販売LOUIS VUITTON) バッグ、財布、 小物類などでございます。
弊社は「信用第一」をモットーにお客様にご満足頂けるよう、
送料は無料です(日本全国)! ご注文を期待しています!
下記の連絡先までお問い合わせください。
是非ご覧ください!
激安、安心、安全にお届けします.品数豊富な商
商品数も大幅に増え、品質も大自信です
100%品質保証!満足保障!リピーター率100%!

# Illikebuisse smnzp 2021/07/04 8:34 www.pharmaceptica.com
purchase peptides tadalafil https://pharmaceptica.com/

# re: ?WPF?Window?????????????????????????????????????? 2021/08/08 0:18 hydroxycloraquine
where to get chloroquine https://chloroquineorigin.com/# hydroxychloroquine hcq

# stromectol ivermectin 3 mg 2021/09/28 18:01 MarvinLic
ivermectin generic https://stromectolfive.com/# ivermectin buy nz

# ivermectin 50 mg 2021/11/01 21:35 DelbertBup
ivermectin 400 mg brands http://stromectolivermectin19.online# ivermectin buy
ivermectin rx

# how much does ivermectin cost 2021/11/03 1:27 DelbertBup
purchase oral ivermectin http://stromectolivermectin19.online# ivermectin cream
ivermectin 3mg

# ivermectin cream cost 2021/11/04 12:51 DelbertBup
ivermectin 10 ml http://stromectolivermectin19.com/# generic ivermectin cream
ivermectin buy australia

# sildenafil 20 mg tablet 2021/12/09 19:40 JamesDat
http://iverstrom24.com/# scabies stromectol dosage

# buy bimatoprost 2021/12/12 7:56 Travislyday
https://bimatoprostrx.com/ bimatoprost

# baricitinib https://baricitinibrx.com
where to buy baricitinib
2021/12/12 16:29 Buksdldj
baricitinib https://baricitinibrx.com
where to buy baricitinib


# careprost for sale 2021/12/13 23:16 Travislyday
http://plaquenils.online/ hydroxychloroquine 30 mg

# bimatoprost generic 2021/12/14 18:34 Travislyday
http://stromectols.online/ buy ivermectin cream

# bimatoprost buy 2021/12/16 7:40 Travislyday
https://plaquenils.com/ cost of plaquenil in canada

# ivermectin 8 mg 2021/12/18 0:04 Eliastib
xtyxyj https://stromectolr.com ivermectin 1 cream

# SpuuvxJbSCMmAYWAtRH 2022/04/19 13:52 johnanz
http://imrdsoacha.gov.co/silvitra-120mg-qrms

# buy doxycycline online without prescription https://antibiotic.best/ 2022/10/08 8:54 Antibiotic
buy doxycycline online without prescription https://antibiotic.best/

# aralen price usa 2022/12/25 7:41 MorrisReaks
purchase hydroxychloroquine http://www.hydroxychloroquinex.com/

# earch our drug database. Get information now.
https://edonlinefast.com
Get information now. Medicament prescribing information. 2023/02/17 10:59 EdOnline
earch our drug database. Get information now.
https://edonlinefast.com
Get information now. Medicament prescribing information.

# buy cytotec online - https://cytotecsale.pro/# 2023/04/29 4:34 Cytotec
buy cytotec online - https://cytotecsale.pro/#

# metronidazole over the counter https://overthecounter.pro/# 2023/05/08 22:37 OtcJikoliuj
metronidazole over the counter https://overthecounter.pro/#

# best ed pills online https://edpill.pro/# - erectile dysfunction drug 2023/06/27 14:32 EdPills
best ed pills online https://edpill.pro/# - erectile dysfunction drug

# buy generic doxycycline https://doxycycline.forum/ doxycycline tetracycline 2023/11/25 13:08 Doxycycline
buy generic doxycycline https://doxycycline.forum/ doxycycline tetracycline

# farmacie online affidabili https://farmaciait.pro/ farmacia online 2023/12/04 10:08 Farmacia
farmacie online affidabili https://farmaciait.pro/ farmacia online

# new treatments for ed https://edpills.tech/# erectile dysfunction pills 2023/12/23 8:06 EdPills
new treatments for ed https://edpills.tech/# erectile dysfunction pills

# lana rhoades full video - https://lanarhoades.pro/ lana rhoades pics
2024/03/06 17:50 LanaRho
lana rhoades full video - https://lanarhoades.pro/ lana rhoades pics


# eva elfie new video https://evaelfie.site/ eva elfie hd
2024/03/07 6:19 EvaElfie
eva elfie new video https://evaelfie.site/ eva elfie hd


# game c&#7901; b&#7841;c online uy t&#237;n http://casinvietnam.shop/# - web c&#7901; b&#7841;c online uy t&#237;n
2024/04/17 17:51 Vietnam
game c&#7901; b&#7841;c online uy t&#237;n http://casinvietnam.shop/# - web c&#7901; b&#7841;c online uy t&#237;n


Post Feedback

タイトル
名前
Url:
コメント