###前提・実現したいこと
作成したユーザーコントロールに、XAML側のみで異なる値をバインドしたい。
動的に内容が変わらない共通するインターフェースのため、コードは使わず値のみのバインドしたほうがメンテもわかりやすいと思っております。
またオマケですが、複数のユーザーコントロールを作成する場合、ネーミングやメインウィンドウ側でどのように違うユーザーコントロールを指定するのか、その方法も教えていただけると幸いです。
###想定している設計
XML
1<!--ユーザーコントロール--> 2 3<UserControl x:Class="UserControl1" 4 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 5 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 6 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 7 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 8 xmlns:local="clr-namespace:WPF_practice" 9 mc:Ignorable="d" 10 d:DesignHeight="20" d:DesignWidth="50"> 11 <Grid> 12 13 <!--2つのコントロールを区別する方法--> 14 <StackPanel Orientation="Vertical"> 15 <TextBlock Height="20" TextAlignment="Center" FontSize="10" Text="{Binding}"/> 16 <!--Textの部分にバインドする--> 17 </StackPanel> 18 19 <StackPanel Orientation="Vertical"> 20 <TextBlock Height="10" TextAlignment="Center" FontSize="10" Text="{Binding}"/> 21 <!--Textの部分にバインドする--> 22 </StackPanel> 23 24 </Grid> 25</UserControl>
XML
1<!--メインウィンドウ--> 2<Window x:Class="MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 7 xmlns:local="clr-namespace:WPF_practice" 8 mc:Ignorable="d" 9 Title="MainWindow" Height="350" Width="525"> 10 11 <Grid> 12 <local:UserControl1 Text="Control1_A"/> 13 <local:UserControl1 Text="Control1_B"/> 14 <local:UserControl1 Text="Control2_A"/> 15 <local:UserControl1 Text="Control1_C"/> 16 <!--上記のように、コードは使わずバインドしたい--> 17 </Grid> 18</Window> 19
回答1件
あなたの回答
tips
プレビュー