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
1 using CommunityToolkit . Mvvm . ComponentModel ;
2 using CommunityToolkit . Mvvm . Input ;
3 using System . Windows ;
4 using System . Windows . Input ;
5
6 namespace 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 }
INotifyPropertyChanged
・ICommand
実装は↓を使用。
NuGet Gallery | CommunityToolkit.Mvvm 7.0.3
Introduction to the MVVM Toolkit - Windows Community Toolkit | Microsoft Docs