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

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

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

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

WPF

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

Q&A

1回答

3761閲覧

WPFでWindowのHeaderのTextBoxの内容を書き換えたい。

cancat

総合スコア313

C#

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

WPF

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

0グッド

0クリップ

投稿2016/09/05 05:37

こんにちは。
Windows10でWPFのアプリケーションを開発しています。
Visual Studio 2015 Communityを使っています。

###前提・実現したいこと
WindowのHeaderをつくろうとしています。
Styleに設定したTextBoxにcsからアクセスし、内容を書き換えたいです。

###発生している問題・エラーメッセージ
アクセスできずにいます。

###該当のソースコード

xaml

1App.xaml 2<Application x:Class="WpfApplication1.App" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="clr-namespace:WpfApplication1" 6 StartupUri="MainWindow.xaml"> 7 <Application.Resources> 8 <!-- Windowの共通表示--> 9 <Style x:Key="CommonWindowStyle" TargetType="Window"> 10 <Setter Property="Template"> 11 <Setter.Value> 12 <ControlTemplate TargetType="Window"> 13 <Grid Margin="5"> 14 <Grid.RowDefinitions> 15 <RowDefinition Height="Auto"/> 16 <RowDefinition Height="*"/> 17 </Grid.RowDefinitions> 18 <!-- ヘッダー --> 19 <Grid Grid.Row="0" Background="#FF24B3D5"> 20 <!-- 共通で使用するオブジェクト --> 21 <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Bottom"> 22 <!-- アプリケーションのタイトル --> 23 <TextBlock 24 Text="ApplicationTitle" 25 Foreground="White" 26 FontWeight="Bold" 27 FontSize="24" 28 Margin="5"/> 29 <!--・Icon--> 30 <Image x:Name="IconImage" MinWidth="50" Margin="2.5" Stretch="UniformToFill" Width="80" Height="80"/> 31 <TextBlock x:Name="UserName" MinWidth="200" Margin="2.5" Foreground="White" FontSize="20"/> 32 </StackPanel> 33 </Grid> 34 <!-- コンテンツ部分 --> 35 <Border Grid.Row="1" Background="{TemplateBinding Background}"> 36 <ContentPresenter /> 37 </Border> 38 </Grid> 39 </ControlTemplate> 40 </Setter.Value> 41 </Setter> 42 </Style> 43 44 </Application.Resources> 45</Application> 46 47 48MainWindow.xaml 49<Window x:Class="WpfApplication1.MainWindow" 50 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 51 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 52 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 53 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 54 xmlns:local="clr-namespace:WpfApplication1" 55 Style="{Binding Source={StaticResource CommonWindowStyle}}" 56 mc:Ignorable="d" 57 Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> 58 <Grid> 59 60 </Grid> 61</Window> 62 63MainWindow.xaml.cs 64using System.Windows; 65 66namespace WpfApplication1 { 67 public partial class MainWindow : Window { 68 public MainWindow() { 69 InitializeComponent(); 70 } 71 72 private void Window_Loaded(object sender, RoutedEventArgs e) { 73 //ここで、App.xamlのImage x: Name = "IconImage", TextBlock x: Name = "UserName"を呼びたい。 74 75 } 76 } 77} 78

###補足情報(言語/FW/ツール等のバージョンなど)
Microsoft Visual Studio Community 2015
Version 14.0.25424.00 Update 3
Microsoft .NET Framework
Version 4.6.01038

インストールしているバージョン:Community

Visual C# 2015 00322-20000-00000-AA575
Microsoft Visual C# 2015

です。
よろしくお願いします。

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

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

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

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

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

guest

回答1

0

今更の投稿で失礼します。
WPFはよく知らないのですが、純粋にApp.xamlのノードを辿っていって取得できましたよ。

csharp

1var setter = (Setter)Style.Setters[0]; 2var template = (ControlTemplate)setter.Value; 3var grid = (Grid)template.LoadContent(); 4 5var iconImage = (Image)grid.FindName("IconImage"); 6var userName = (TextBlock)grid.FindName("UserName");

投稿2016/09/16 16:18

htsign

総合スコア870

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

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

cancat

2016/09/20 01:17

ありがとうございます。 結局今回は間に合わなかったので、Styleを使うのをやめてしまいました。 残念ですが感謝します。 検証できないので、このままですみません。
htsign

2016/09/20 03:27

なるほど。 お答えするのが間に合わずすみませんでした。
cancat

2016/09/20 04:48

いえいえ。こちらこそ恐縮です。 先週の月曜くらいまでは待ったり調べたりしていたのですが、時間切れと決断しました。 Style、次には使いたいなーと思います。 ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問