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

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

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

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

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

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

Q&A

解決済

1回答

1920閲覧

別のxamlファイルにBindingしたい

horaihorai

総合スコア14

C#

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

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

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

0グッド

1クリップ

投稿2021/07/07 12:30

編集2021/07/08 02:27

WPFに関して、別のxamlファイルにBindingしたいです。

現在、

Left.xaml

. . <StackPanel Name="Width"> <Button Command="{Binding Command}"/> <StackPanel Width="{Binding LeftWidth}"> . . </StackPanel> </StackPanel> . .

**Left.xaml.cs **↓

. public partical class Left { . . Width.DataContext = new ViewModel(){LeftWidth = 100, RightWidth= 100}; . . } class ViewModel : INotifyPropertyChanged { . . } class Command : ICommand { . . }

のような形でLeft.xamlのボタンを押すとwidthのサイズが変わるところまでできたのですが、

以下のAll.xamlファイルの中で、Left.xaml(上記)のボタンを押した場合に、Left.xamlのwidthだけでなくRight側のwidthも変更したいのですが、うまくできません。

All.xaml

. . <StackPanel> <userControls: Left/> </StackPanel> <StackPanel> <userControls:Right Width="{Binding RightWidth}"/> </StackPanel> . .

Left.xamlの中のボタンを押してRight.xaml側にも値を渡すにはどのようにすれば良いでしょうか。

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

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

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

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

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

TN8001

2021/07/07 13:38 編集

長いのを省略するのは仕方ないですが、 class ViewModel() { } こんな書き方はできないし、ちょっと雑すぎませんか。 class Leftで Width.DataContext = new ViewModel() とするのは変更できないんですかね? AllのDataContext(ViewModel)自体かそのプロパティのひとつに設定すれば、そのまま引き継がれるのですが。
horaihorai

2021/07/08 02:29

ご指摘ありがとうございます。 class Leftで Width.DataContext = new ViewModel() とするのを変更し、AllのDataContextの方で設定することで解決しました。 全体的に私の理解が乏しかったため、勉強し直します。ありがとうございました。
TN8001

2021/07/08 03:38

> ご指摘ありがとうございます。 きつめの言葉に感じていたとしたらお詫びします。 今後も萎縮せずお気軽にご利用ください^^
guest

回答1

0

ベストアンサー

horaihoraiさんのほうではすでに解決されていますが、第三者視点ではわかりにくいところもあるのでまとめさせていただきます。

xml:Left.xaml

1<UserControl 2 x:Class="Questions348249.Left" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 5 <StackPanel Name="Width"> 6 <Button Command="{Binding Command}" Content="Button" /> 7 <StackPanel Width="{Binding LeftWidth}"> 8 <Rectangle Height="30" Fill="Red" /> 9 </StackPanel> 10 </StackPanel> 11</UserControl>

xml:Right.xaml

1<UserControl 2 x:Class="Questions348249.Right" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 5 <StackPanel Name="Width"> 6 <Button Command="{Binding LeftRight.Command}" Content="Button" /> 7 <StackPanel Width="{Binding LeftRight.RightWidth}"> 8 <Rectangle Height="30" Fill="Blue" /> 9 </StackPanel> 10 </StackPanel> 11</UserControl>

xml:MainWindow.xaml

1<Window 2 x:Class="Questions348249.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:userControls="clr-namespace:Questions348249" 6 Width="800" 7 Height="450"> 8 <StackPanel HorizontalAlignment="Center" Orientation="Horizontal"> 9 10 <!-- こうしてもいいし --> 11 <userControls:Left DataContext="{Binding LeftRight}" /> 12 13 <!-- こうするならRightで Width="{Binding LeftRight.RightWidth}" となる --> 14 <userControls:Right /> 15 </StackPanel> 16</Window>

cs

1using CommunityToolkit.Mvvm.ComponentModel; 2using CommunityToolkit.Mvvm.Input; 3using System.Windows; 4using System.Windows.Input; 5 6namespace Questions348249 7{ 8 class LeftRightViewModel : ObservableObject 9 { 10 public double LeftWidth { get => leftWidth; set => SetProperty(ref leftWidth, value); } 11 private double leftWidth; 12 public double RightWidth { get => rightWidth; set => SetProperty(ref rightWidth, value); } 13 private double rightWidth; 14 public ICommand Command { get; } 15 16 public LeftRightViewModel() 17 { 18 Command = new RelayCommand(() => 19 { 20 LeftWidth += 10; 21 RightWidth += 5; 22 }); 23 } 24 } 25 26 class MainViewModel 27 { 28 public LeftRightViewModel LeftRight { get; } 29 = new LeftRightViewModel { LeftWidth = 100, RightWidth = 100, }; 30 } 31 32 public partial class MainWindow : Window 33 { 34 public MainWindow() 35 { 36 InitializeComponent(); 37 38 // 「AllのDataContext(ViewModel)自体」 に設定した場合 39 //DataContext = new LeftRightViewModel(); 40 41 // 「そのプロパティのひとつに設定」 した場合 42 DataContext = new MainViewModel(); 43 } 44 } 45}

INotifyPropertyChangedICommand実装は↓を使用。
NuGet Gallery | CommunityToolkit.Mvvm 7.0.3

Introduction to the MVVM Toolkit - Windows Community Toolkit | Microsoft Docs

投稿2021/07/08 03:38

編集2023/07/28 13:31
TN8001

総合スコア9396

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問