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

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

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

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

WPF

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

Q&A

解決済

4回答

11161閲覧

WPFで、クラスからtextblockなどを編集したい

退会済みユーザー

退会済みユーザー

総合スコア0

C#

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

WPF

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

0グッド

0クリップ

投稿2016/09/15 03:22

編集2016/09/15 07:43

###前提・実現したいこと
C#のWPFでUIアプリの開発を行っています。

クラスを作り、そこからtextblockなどのUIを操作したいと考えております。
以下のコードでは、ボタンを押すことでtextblockのTextが変更されることを期待していますが、思い通りになりません。
###該当のソースコード

C#

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Windows; 7using System.Windows.Controls; 8using System.Windows.Data; 9using System.Windows.Documents; 10using System.Windows.Input; 11using System.Windows.Media; 12using System.Windows.Media.Imaging; 13using System.Windows.Navigation; 14using System.Windows.Shapes; 15 16namespace WpfApplication1 17{ 18 public partial class MainWindow : Window 19 { 20 public MainWindow() 21 { 22 InitializeComponent(); 23 } 24 25 class TextChange:MainWindow 26 { 27 public void Change(string x) 28 { 29 textBlock.Text = x; 30 } 31 } 32 private void button_Click(object sender, RoutedEventArgs e) 33 { 34 string ChangeTo = "Changed"; 35 TextChange hogehoge = new TextChange(); 36 hogehoge.Change(ChangeTo); 37 38 } 39 } 40}

xaml

1<Window x:Class="WpfApplication1.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:WpfApplication1" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="100" Width="200"> 9 <Grid> 10 <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="68,10,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/> 11 <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="54,36,0,0" VerticalAlignment="Top" Width="76" Click="button_Click"/> 12 13 </Grid> 14</Window>

###実行結果
ボタンを押しても何も変わりません。

また、気になる点として、実行後何もしないでウィンドウのx印で閉じるとそのままデバッグが終了しますが、
ボタンを押してからx印で閉じると、ウィンドウは消えますがデバッグが実行中になっています。

###質問事項
①上のコードでは、なぜ期待した結果にならないのか。
②どのようにすれば望んだ挙動になるのか。

その2点をお伺いしたく質問させていただきました。

###補足
バインディングでやる方法も考えたのですが、その方法だと私の望んだ回答を得られないかもしれません。
というのは、長くなるのもありこちらでは仮のプログラムとしてTextの書き換えを行っています。
クラスでグリッドなどに図形を描いたりといったことをバインディングで実装できるのでしょか…?

C#どころかプログラムに関しても初心者です。盛大な勘違いをしてるかもしれないのでご容赦ください。

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

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

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

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

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

guest

回答4

0

ベストアンサー

①派生しているのでTextChangeのTextBlockと呼び出し元のMainWindowのTextBlockは全くの別物です
②button_ClickでOwnerプロパティを表示されているMainWindowに設定してやります。TextChangeでOwner(表示されているMainWindow)のTextBlockを書き換えます

投稿2016/09/15 09:10

dn315

総合スコア200

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

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

退会済みユーザー

退会済みユーザー

2016/09/15 09:19

期待通りの処理を実装することができました。ありがとうございます。 今後のためにもOwnerプロパティについて深く調べてみようと思います。
guest

0

TextChangeでMainWindowを派生してるからじゃないでしょうか

C#

1namespace WpfApplication1 2{ 3 public partial class MainWindow : Window 4 { 5 public MainWindow() 6 { 7 InitializeComponent(); 8 } 9 10 class TextChange : MainWindow 11 { 12 public void Change(string x) 13 { 14 ((MainWindow)this.Owner).textBlock.Text = x; 15 } 16 } 17 18 private void button_Click(object sender, RoutedEventArgs e) 19 { 20 string ChangeTo = "Changed"; 21 TextChange hogehoge = new TextChange(); 22 hogehoge.Owner = this; 23 hogehoge.Change(ChangeTo); 24 } 25 } 26}

投稿2016/09/15 08:55

編集2016/09/15 09:00
dn315

総合スコア200

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

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

0

Bindingを使ってみたらどうでしょうか?

XAML

1<Window x:Class="WpfApplication1.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:WpfApplication1" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="100" Width="200"> 9 <Grid> 10 <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="68,10,0,0" TextWrapping="Wrap" Text="{Binding Text}" VerticalAlignment="Top"/> 11 <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="54,36,0,0" VerticalAlignment="Top" Width="76" Click="button_Click"/> 12 </Grid> 13</Window>

C#

1using System; 2using System.Windows; 3using System.ComponentModel; 4using System.Runtime.CompilerServices; 5 6namespace WpfApplication1 7{ 8 internal class TextChangeClass : INotifyPropertyChanged 9 { 10 private string _text; 11 12 public string Text 13 { 14 get 15 { 16 return _text; 17 } 18 set 19 { 20 _text = value; 21 NotifyPropertyChanged(); 22 } 23 } 24 25 public event PropertyChangedEventHandler PropertyChanged; 26 27 public void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 28 { 29 if (PropertyChanged != null) 30 { 31 PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 32 } 33 } 34 } 35 36 /// <summary> 37 /// MainWindow.xaml の相互作用ロジック 38 /// </summary> 39 /// 40 public partial class MainWindow : Window 41 { 42 private TextChangeClass TextChange { set; get; } 43 44 public MainWindow() 45 { 46 InitializeComponent(); 47 TextChange = new TextChangeClass(); 48 TextChange.Text = "TextBlock"; 49 this.DataContext = TextChange; 50 } 51 52 private void button_Click(object sender, RoutedEventArgs e) 53 { 54 this.TextChange.Text = "Change"; 55 } 56 } 57} 58

投稿2016/09/15 06:30

編集2016/09/15 06:32
dn315

総合スコア200

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

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

退会済みユーザー

退会済みユーザー

2016/09/15 07:48

Textの書き換えなどであればそれで可能かもしれませんが、バインディングでは応用が利かないのではないかと思っています。 質問の仕方が少し悪かったかもしれません。 質問を加筆しましたので、そちらもご覧ください。
guest

0

うーん、とりあえず、XAML 部分もお願いできますか?

基本的にTextBlock コントロールに textBlock という名前がついているのであれば、
それで出るとは思うのですが。

投稿2016/09/15 03:36

mugicya

総合スコア1046

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

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

退会済みユーザー

退会済みユーザー

2016/09/15 03:42

Xamlの部分も追加いたしました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問