前提
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
回答1件
あなたの回答
tips
プレビュー