.NET 5のWPFプロジェクトで外部から複数の値をバインドさせたいUserControlを作っています。そのためUserControlに独自型Texts
の依存関係プロパティValue
を作成し、UserControlの利用者となる親画面のxamlからValue
をセットすることで、UserControlのコードビハインドで値を加工したりした上でUserControlのViewに反映させたいと考えています。
しかし、以下のコードで親画面(ここではコードビハインド)からTexts
を作成してバインドさせても、画面に内容が反映されません。
独自型のバインドは不可能で、バインドしたい値が複数あったら1つ1つ依存関係プロパティを設定してバインドしなければならないのでしょうか?
※以下コードでは親画面側でReactiveProperty<Texts>
を使ってバインドさせているつもりです。
Texts.cs
c#
using System.Windows; using System.Windows.Controls; namespace WpfApp1 { public class Texts { public int A { get; set; } = 5; public string B { get; set; } = ""; public double? C { get; set; } } }
UserControl.xaml
xaml
<UserControl x:Class="WpfApp1.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" d:DesignHeight="200" d:DesignWidth="200"> <Grid Background="White"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Text="{Binding Value.A}" Foreground="Black"/> <TextBlock Grid.Row="1" Text="{Binding Value.B}" Foreground="Black"/> <TextBlock Grid.Row="2" Text="{Binding Value.C}" Foreground="Black"/> </Grid> </UserControl>
UserControl.xaml.cs
c#
using System.Windows; using System.Windows.Controls; namespace WpfApp1 { /// <summary> /// UserControl1.xaml の相互作用ロジック /// </summary> public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( "Value", typeof(Texts), typeof(UserControl1), new PropertyMetadata(new Texts())); public Texts Value { get { return (Texts)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } } }
MainWindow.xaml
xaml
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <local:UserControl1 Grid.Column="0" Value="{Binding ValueA.Value}"/> <local:UserControl1 Grid.Column="1" Value="{Binding ValueB.Value}"/> <local:UserControl1 Grid.Column="2" Value="{Binding ValueC.Value}"/> </Grid> </Window>
MainWindow.xaml.cs
c#
using Reactive.Bindings; using System.Windows; namespace WpfApp1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ValueA = new ReactiveProperty<Texts>(new Texts { A = 1, B = "Ab", C = 10.0 }); ValueB = new ReactiveProperty<Texts>(new Texts { A = 2, B = "Bb", C = 222.22 }); ValueC = new ReactiveProperty<Texts>(new Texts { A = 3, B = "Cb", C = null }); } public ReactiveProperty<Texts> ValueA { get; } public ReactiveProperty<Texts> ValueB { get; } public ReactiveProperty<Texts> ValueC { get; } } }
まだ回答がついていません
会員登録して回答してみよう