質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

1回答

2529閲覧

C# WPF Pageから親Windowの関数をコールしたい

Hottopia

総合スコア16

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2023/03/09 23:49

前提

C# (WPF)に関する質問です。
タイトルの通り、Windowに包括されているPageから、親Windowの関数をコールする方法を考えております。

実現したいこと

子Pageから親Windowの関数をコールしたり、コントロールのプロパティを取得、設定したりしたい。

該当のソースコード

MainWindow.xaml

XAML

1<Window x:Class="Test.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:Test" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="768" Width="1024" 9 Background="Black"> 10 <Grid> 11 <Frame Name="Frame" NavigationUIVisibility="Hidden"/> 12 </Grid> 13</Window>

MainWindow.xaml.cs

C#

1using System; 2using System.Collections.Generic; 3using System.Globalization; 4using System.IO; 5using System.Linq; 6using System.Text; 7using System.Threading.Tasks; 8using System.Windows; 9using System.Windows.Controls; 10using System.Windows.Data; 11using System.Windows.Documents; 12using System.Windows.Input; 13using System.Windows.Interop; 14using System.Windows.Media; 15using System.Windows.Media.Imaging; 16using System.Windows.Navigation; 17using System.Windows.Shapes; 18using System.Windows.Threading; 19 20namespace Test 21{ 22 /// <summary> 23 /// Interaction logic for MainWindow.xaml 24 /// </summary> 25 public partial class MainWindow : Window 26 { 27 public MainWindow() 28 { 29 InitializeComponent(); 30 Sound.Init(); 31 Uri uri = new Uri(@"OperationParentTest.xaml", UriKind.Relative); 32 Frame.Source = uri; 33 } 34 35 public void TestMethod() 36 { 37 //子Pageから呼び出したい関数 38 } 39 } 40}

OperationParentTest.xaml

XAML

1<Page x:Class="Test.OperationParentTest" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:local="clr-namespace:Test" 7 mc:Ignorable="d" 8 d:DesignHeight="450" d:DesignWidth="800" 9 Title="OperationParentTest" 10 Background="White"> 11 12 <Grid> 13 <Button x:Name="Button1" 14 Content="CallParentMethod" 15 Height="50" Width="150" 16 Click="Button1_Click"/> 17 </Grid> 18</Page>

OperationParentTest.xaml.cs

C#

1using System; 2using System.Collections.Generic; 3using System.Text; 4using System.Windows; 5using System.Windows.Controls; 6using System.Windows.Data; 7using System.Windows.Documents; 8using System.Windows.Input; 9using System.Windows.Media; 10using System.Windows.Media.Imaging; 11using System.Windows.Navigation; 12using System.Windows.Shapes; 13 14namespace Test 15{ 16 /// <summary> 17 /// ParentOperationTest.xaml の相互作用ロジック 18 /// </summary> 19 public partial class OperationParentTest : Page 20 { 21 public OperationParentTest() 22 { 23 InitializeComponent(); 24 } 25 26 private void Button1_Click(object sender, RoutedEventArgs e) 27 { 28 var parentWindow = (MainWindow)Window.GetWindow(this); 29 30 // parentWindow.TestMethod() ←のようなことができると思ってました。 31 } 32 } 33} 34

補足情報(FW/ツールのバージョンなど)

Visual Studio 2022
C#10.0
WPF

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TN8001

2023/03/10 00:58

> var parentWindow = (MainWindow)Window.GetWindow(this); > // parentWindow.TestMethod() ←のようなことができると思ってました。 できると思いますがどうできないのですか?(キャストエラー??)
Hottopia

2023/03/10 08:48

あれ?できました。 なんでできなかったのかわかりませんが、お騒がせしてもうしわけありません。
guest

回答1

0

自己解決

できました。勘違いでした。

投稿2023/03/10 08:48

Hottopia

総合スコア16

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問