teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

要件整理のうえ、修正

2017/02/05 04:27

投稿

Tak1wa
Tak1wa

スコア4791

answer CHANGED
@@ -1,7 +1,7 @@
1
1
  こんにちは。
2
2
 
3
- DataContextは子要素引き継がれるので、バインドさる場合が簡単です
3
+ Text色々な役割を持たせすぎてしまいますのでこれは好ましくありま
4
- その場合はUserControlのTextはなくDataContextに設定するだけでUserControl側の修正は不要です
4
+ UserControl自体を別作成するレイアウトを切り替えるプロパティを別途用意しましょう
5
5
 
6
6
  ```XML
7
7
  <Window x:Class="MainWindow"
@@ -9,22 +9,65 @@
9
9
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
10
10
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
11
11
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
12
- xmlns:local="clr-namespace:WpfApp10"
12
+ xmlns:local="clr-namespace:VisualBasic.Wpf.Samples"
13
13
  mc:Ignorable="d"
14
- Title="MainWindow" Height="350" Width="525">
14
+ Title="MainWindow" Height="300" Width="300">
15
+ <StackPanel Orientation="Horizontal">
16
+ <local:UserControl1 Text="Control1_A" LayoutType="Control1" />
17
+ <local:UserControl1 Text="Control1_B" LayoutType="Control1" />
18
+ <local:UserControl1 Text="Control2_A" LayoutType="Control2" />
19
+ <local:UserControl1 Text="Control1_C" LayoutType="Control1" />
20
+ </StackPanel>
21
+ </Window>
22
+ ```
23
+ ```XML
24
+ <UserControl x:Class="UserControl1"
25
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
26
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
27
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
28
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
29
+ mc:Ignorable="d"
30
+ x:Name="uc"
31
+ d:DesignHeight="20" d:DesignWidth="50">
15
32
  <Grid>
33
+ <ContentControl VerticalAlignment="Top" Width="100" Margin="10" VerticalContentAlignment="Center">
34
+ <ContentControl.Style>
16
- <local:UserControl1 DataContext="Control1"/>
35
+ <Style TargetType="ContentControl">
36
+ <Style.Triggers>
37
+ <!--2つのコントロールを区別する方法 → LayouType依存関係プロパティで切り替える例-->
38
+ <DataTrigger Binding="{Binding LayoutType, ElementName=uc}" Value="Control1">
39
+ <Setter Property="Template">
40
+ <Setter.Value>
41
+ <ControlTemplate>
17
- <local:UserControl1 DataContext="Control2"/>
42
+ <Border Background="#FF00A0E9" Height="40">
43
+ <TextBlock VerticalAlignment="Center" TextAlignment="Center"
44
+ Text="{Binding Text, ElementName=uc}"/>
45
+ </Border>
46
+ </ControlTemplate>
47
+ </Setter.Value>
48
+ </Setter>
49
+ </DataTrigger>
50
+ <DataTrigger Binding="{Binding LayoutType, ElementName=uc}" Value="Control2">
51
+ <Setter Property="Template">
52
+ <Setter.Value>
53
+ <ControlTemplate>
54
+ <Border Background="#FFEE7642" Height="20">
55
+ <TextBlock VerticalAlignment="Center"
56
+ TextAlignment="Center"
57
+ Text="{Binding Text, ElementName=uc}"/>
58
+ </Border>
59
+ </ControlTemplate>
60
+ </Setter.Value>
61
+ </Setter>
62
+ </DataTrigger>
63
+ </Style.Triggers>
64
+ </Style>
65
+ </ContentControl.Style>
66
+ </ContentControl>
18
67
  </Grid>
19
- </Window>
68
+ </UserControl>
20
69
  ```
21
-
22
- ---
23
-
24
- XAML上で値を指定したい場合などは依存関係プロパティをUserControlに用意して、それを子要素がバインドする方法があります。
25
-
26
-
27
- ```VB.NET
70
+ ```VB
28
71
  Public Class UserControl1
29
72
  Public Property Text() As String
30
73
  Get
@@ -36,29 +79,15 @@
36
79
  End Property
37
80
  Public Shared ReadOnly TextProperty As DependencyProperty = DependencyProperty.Register("Text", GetType(String), GetType(UserControl1), New PropertyMetadata(String.Empty))
38
81
 
82
+ Public Property LayoutType() As String
83
+ Get
84
+ Return CType(Me.GetValue(LayoutTypeProperty), String)
85
+ End Get
86
+ Set(ByVal value As String)
87
+ Me.SetValue(LayoutTypeProperty, value)
88
+ End Set
89
+ End Property
90
+ Public Shared ReadOnly LayoutTypeProperty As DependencyProperty = DependencyProperty.Register("LayoutType", GetType(String), GetType(UserControl1), New PropertyMetadata(String.Empty))
91
+
39
92
  End Class
40
- ```
41
-
42
- ```XML
43
- <UserControl x:Class="UserControl1"
44
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
45
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
46
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
47
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
48
- x:Name="uc"
49
- mc:Ignorable="d"
50
- d:DesignHeight="300" d:DesignWidth="300">
51
- <Grid>
52
-
53
- <!--2つのコントロールを区別する方法-->
54
- <StackPanel Orientation="Vertical">
55
- <TextBlock Height="10" TextAlignment="Center" FontSize="7" Text="{Binding Text, ElementName=uc}"/>
56
- </StackPanel>
57
-
58
- <StackPanel Orientation="Vertical">
59
- <TextBlock Height="20" TextAlignment="Center" FontSize="10" Text="{Binding Text, ElementName=uc}"/>
60
- </StackPanel>
61
-
62
- </Grid>
63
- </UserControl>
64
93
  ```