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

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

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

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

WPF

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

Q&A

解決済

1回答

6494閲覧

WPFの複数のwindowsで共通のheaderを作りたいです。

k-hi

総合スコア21

C#

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

WPF

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

0グッド

0クリップ

投稿2016/08/09 05:59

編集2016/08/09 06:03

###WPFのStyleにimageを使いたいです。
こんにちは。

Windows10, Visual Studio 2015 community, WPFで複数の画面をもつシステムを作っています。
windowsで共通のheaderを作りたいと考えています。

https://code.msdn.microsoft.com/windowsdesktop/WPFWindow-c471ff75
を参考に作業しています。
NuGetで、ReactivePropertyを追加しています。

###発生している課題
button, textblockは表示できますがimageを表示できません。
コードは次のようなものです。
Bindingが初めてで、コードが複雑で解析できません。
アドバイスよろしくお願いします。

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

WPF(App.xaml)

1<Application.Resources> 2 <Style x:Key="CommonWindowStyle" TargetType="Window"> 3 <Setter Property="Template"> 4 <Setter.Value> 5 <ControlTemplate TargetType="Window"> 6 <Grid Margin="5"> 7 <Grid.RowDefinitions> 8 <RowDefinition Height="Auto"/> 9 <RowDefinition Height="*"/> 10 </Grid.RowDefinitions> 11 <!-- ヘッダー --> 12 <Grid Grid.Row="0"> 13 <Grid.Background> 14 <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5"> 15 <GradientStop Color="#FFC129FF" Offset="0" /> 16 <GradientStop Color="#FF800012" Offset="1" /> 17 </LinearGradientBrush> 18 </Grid.Background> 19 <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Bottom"> 20 <TextBlock 21 Text="testprogram" 22 Foreground="White" 23 FontWeight="Bold" 24 FontSize="24" 25 Margin="5"/> 26 <Button MinWidth="50" Content="{Binding CommonAButton.Label.Value}" Command="{Binding CommonAButton.Command}" Margin="2.5"/> 27 <Image MinWidth="50" Source="{Binding CommonIconImage}" Margin="2.5" Stretch="Uniform"/> 28 <Rectangle MinWidth="50" Fill="{Binding CommonRectangle}" Margin="2.5"/> 29 <TextBlock MinWidth="200" Text="{Binding CommonName.Text}" Margin="2.5" Foreground="White" FontSize="20"/> 30 </StackPanel> 31 </Grid> 32 <!-- コンテンツ部分 --> 33 <Border Grid.Row="1" Background="{TemplateBinding Background}"> 34 <ContentPresenter /> 35 </Border> 36 </Grid> 37 </ControlTemplate> 38 </Setter.Value> 39 </Setter> 40 </Style> 41</Application.Resources>

C#(WindowViewModelBase.cs)

1using Reactive.Bindings; 2using System.Windows.Controls; 3using System.Windows.Media; 4using System.Windows.Media.Imaging; 5using System.Windows.Shapes; 6 7namespace test.ViewModels{ 8 /// <summary> 9 /// CommonWindowStyleを適用したWindowのViewModelの基本クラス。 10 /// </summary> 11 public abstract class WindowViewModelBase 12 { 13 /// <summary> 14 /// 左側のボタン 15 /// </summary> 16 public ButtonViewModel CommonAButton { get; set; } 17 18 /// <summary> 19 /// IconImage 20 /// </summary> 21 public Image CommonIconImage { get; set; } 22 23 public Rectangle CommonRectangle { get; set; } 24 25 /// <summary> 26 /// CommonName 27 /// </summary> 28 public TextBlock CommonName { get; set; } 29 30 protected WindowViewModelBase(ButtonViewModel a = null, ButtonViewModel b = null) 31 { 32 this.CommonAButton = a ?? new ButtonViewModel(); 33 34 this.CommonIconImage = new Image(); 35 this.CommonRectangle = new Rectangle(); 36 this.CommonRectangle.Fill = Brushes.Red; 37 this.CommonPatientNameID = new TextBlock(); 38 } 39 } 40 41 /// <summary> 42 /// 画面に表示するボタンを表す 43 /// </summary> 44 public class ButtonViewModel { 45 /// <summary> 46 /// ボタンに表示するテキスト 47 /// </summary> 48 public ReactiveProperty<string> Label { get; private set; } 49 /// <summary> 50 /// ボタンを押した時に実行するコマンド 51 /// </summary> 52 public ReactiveCommand Command { get; private set; } 53 54 public ButtonViewModel(ReactiveProperty<string> label = null, ReactiveCommand command = null) 55 { 56 this.Label = label ?? new ReactiveProperty<string>(); 57 this.Command = command ?? new ReactiveCommand(); 58 } 59 } 60}

xaml(MainWindow.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 xmlns:vm="clr-namespace:test.ViewModels" 8 mc:Ignorable="d" 9 Title="test" Height="700" Width="1050" 10 Style="{Binding Source={StaticResource CommonWindowStyle}}"> 11 <Window.DataContext> 12 <vm:MainViewModel /> 13 </Window.DataContext>

C#(MainWindowViewModel.cs)

1using System; 2using System.Windows; 3using Reactive.Bindings; 4using System.Windows.Controls; 5using System.Windows.Media.Imaging; 6using System.IO; 7using System.Windows.Shapes; 8using System.Windows.Media; 9 10namespace test.ViewModels { 11 public partial class MainWindowViewModel : WindowViewModelBase { 12 /// <summary> 13 /// 画面内に表示するテキスト。ヘッダーの左側のボタンを押した時に変更になる 14 /// </summary> 15 public ReactiveProperty<string> AText { get; private set; } 16 17 public ReactiveProperty<Image> IconImage { get; private set; } 18 19 public ReactiveProperty<Rectangle> RectangleBox { get; private set; } 20 21 public ReactiveProperty<string> NameID { get; private set; } 22 23 24 public MainWindowViewModel() : base() { 25 // プロパティの初期化 26 this.AText = new ReactiveProperty<string>("A count : 0"); 27 28 string photo = @"C:\abc01.jpg"; 29 if (File.Exists(photo)) { 30 Uri uri = new Uri(photo, UriKind.RelativeOrAbsolute); 31 32 //ビットマップのイメージインスタンス 33 BitmapImage source = new BitmapImage(uri); 34 this.IconImage = new ReactiveProperty<Image>(); 35 this.CommonIconImage.Source = source; 36 } 37 this.RectangleBox = new ReactiveProperty<Rectangle>(); 38 this.CommonRectangle.Fill = Brushes.Blue; 39 40 this.NameID = new ReactiveProperty<string>(); 41 42 // 共通部分にあるobjectのcontentsを指定する 43 this.CommonAButton.Label.Value = "backhome"; 44 45 this.CommonName.Text = "testname"; 46 47 // 共通部分にあるボタンを押したときの処理を定義 48 int aCount = 0; 49 this.CommonAButton.Command.Subscribe(_ => { 50 this.AText.Value = "A count : " + ++aCount; 51 } 52 ); 53 int bCount = 0; 54 this.CommonBButton.Command.Subscribe(_ => { 55 this.BText.Value = "B count : " + ++bCount; 56 } 57 ); 58 } 59 } 60}

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

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

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

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

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

guest

回答1

0

ベストアンサー

こんにちは。

Application.ResourceでのImageのSourceにバインドしているのでCommonIconImageがImage型で定義されています。
ImageのSourceにControl.Imageをバインドしているので表示されていないのでしょう。

取り急ぎは以下のようにSourceを渡すことで解決できます。

XAML

1<Image MinWidth="50" Source="{Binding CommonIconImage.Source}" Margin="2.5" Stretch="Uniform"/>

もしくはCommonIconImageをImageSource派生の型かURIなどにすることでしょう。

投稿2016/08/10 23:22

Tak1wa

総合スコア4791

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

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

k-hi

2016/08/15 05:43

こんにちは。なるほど。 App.xamlで、 <Image MinWidth="50" Source="{Binding CommonIconImage}" Margin="2.5" Stretch="Uniform"/> としたので、Sourceなんですね。これで表示できました。ありがとうございます。 CommonIconImageを public ImageSource CommonIconImage { get; set; } にはしてみたのですが、それでは表示できませんでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問