prism WPF フォームに貼り付けたUserControlインスタンスにプロパティをxamlで設定したい。

前提

VisualStudio 2022 で C# WPFプロジェクト
WPFでPrismフレームワークを利用しています。

実現したいこと

MainWindowにUserControlを複数インスタンスで張り付けたときに、
UserControlのそれぞれのインスタンスのプロパティをxamlで設定したい。
ViewModel側にBindingのプロパティを記載したい。

発生している問題・エラーメッセージ

記述の仕方がわかりません。

該当のソースコード

MainWindow.xaml

<Window ・・・> <Grid> <local:PrismUserControl1 /> <==ここで、プロパティ"XXXXLabel"に値を入れて「いいいい」と表示したい。 <local:PrismUserControl1 XXXXLabel={Binging XXXX2Value} /> <==Bindingもしたい。 </Grid> </Window>

PrismUserControl1.xaml

<UserControl ・・・> <Grid> <StackPanel Orientation="Horizontal"> <Label Content="{Binding XXXXLabel}" /> </StackPanel> </Grid> </UserControl>

PrismUserControl1ViewModel.cs

using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Linq; namespace BlankApp.WPF.ViewModels { public class PrismUserControl1ViewModel : BindableBase { private string _xXXXLabel = "ああああ"; public string XXXXLabel { get { return _xXXXLabel; } set { SetProperty(ref _xXXXLabel, value); } } } }

試したこと

PrismUserControl1.xaml.cs にprismを利用しなければ、Bindingプロパティを記載できます。
ViewModel側にプロパティの受け渡しができないので、使いにくいです。

MainWindow.xaml.cs

<Grid> <StackPanel Orientation="Horizontal"> <Views:PrismUserControl1 x:Name="Uc1" Width="100" XXXXValue="sssss"/>   <==これは通ります <Views:PrismUserControl1 x:Name="Uc2" Width="100" XXXXValue="{Binding XXXXValue}" />  <==これは通りません。(名前は便宜上共通) </StackPanel> </Grid>

MainWindowViewModel.cs

private string _xXXXValue = "いいいいいい"; public string XXXXValue { get { return _xXXXValue; } set { SetProperty(ref _xXXXValue, value); } }

PrismUserControl1.xaml

<UserControl ・・・> <Grid> <Label Content="{Binding XXXXValue,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:PrismUserControl1}}}" Width="100" FontSize="50" /> </Grid> </UserControl>

PrismUserControl1.xaml.cs

using System.Windows; using System.Windows.Controls; namespace UC.WPF.Views { /// <summary> /// Interaction logic for PrismUserControl1 /// </summary> public partial class PrismUserControl1 : UserControl { public PrismUserControl1() { InitializeComponent(); } public string XXXXValue { get { return (string)GetValue(XXXXValueProperty); } set { SetValue(XXXXValueProperty, value); } } // Using a DependencyProperty as the backing store for XXXXValue. This enables animation, styling, binding, etc... public static readonly DependencyProperty XXXXValueProperty = DependencyProperty.Register("XXXXValue", typeof(string), typeof(PrismUserControl1), new PropertyMetadata("ああああ")); } }

補足情報(FW/ツールのバージョンなど)

VisualStudio 2022 C# .Net Core 6.0
prism の select container はUnity

コメントを投稿

0 コメント