回答編集履歴

1

見直しキャンペーン中

2023/08/16 05:36

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -10,92 +10,18 @@
10
10
  </DataTemplate>
11
11
  ```
12
12
 
13
- とはいえ長ったらしいですし、事実上こうでいい気がします
13
+ とはいえ長ったらしいですし、事実上こうでいい気がします(下記画像左)
14
14
  ```xml
15
15
  <ItemsControl Foreground="{TemplateBinding TextColor}" ItemsSource="{TemplateBinding ItemsSource}">
16
16
  ```
17
17
 
18
+ 依存関係プロパティを追加したいだけなら、継承でいい気がします(下記画像中央)
19
+
20
+ (ご破算にするようですが)`Foreground`をいじっていいなら、`Style`だけで済んでしまいます(下記画像右)
21
+
18
22
  ---
19
23
 
20
- 依存関係プロパティを追加したいだけなら、継承でいい気がします(継承すべきかラップすべきかは、なかなか難しいところですが)
21
-
22
- (ご破算にするようですが)`Foreground`をいじっていいなら、`Style`だけで済んでしまいます。
23
-
24
- ```xml
25
- <ResourceDictionary
26
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
27
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
28
- xmlns:local="clr-namespace:Qie48og91me277i">
29
-
30
- <Style TargetType="{x:Type local:ScrollView}">
31
- <Setter Property="TextColor" Value="Red" />
32
- <Setter Property="Template">
33
- <Setter.Value>
34
- <ControlTemplate TargetType="{x:Type local:ScrollView}">
35
- <Border
36
- BorderBrush="Black"
37
- BorderThickness="1"
38
- CornerRadius="3">
39
- <ScrollViewer
40
- HorizontalScrollBarVisibility="Disabled"
41
- VerticalScrollBarVisibility="Hidden"
42
- VirtualizingPanel.ScrollUnit="Pixel">
43
- <ItemsControl Foreground="{TemplateBinding TextColor}" ItemsSource="{TemplateBinding ItemsSource}">
44
- <ItemsControl.Template>
45
- <ControlTemplate TargetType="{x:Type ItemsControl}">
46
- <ItemsPresenter />
47
- </ControlTemplate>
48
- </ItemsControl.Template>
49
- <ItemsControl.ItemsPanel>
50
- <ItemsPanelTemplate>
51
- <StackPanel Orientation="Vertical" />
52
- </ItemsPanelTemplate>
53
- </ItemsControl.ItemsPanel>
54
- <ItemsControl.ItemTemplate>
55
- <DataTemplate>
56
- <TextBlock Text="{Binding}" TextAlignment="Center" />
57
- <!--<TextBlock
58
- Foreground="{Binding TextColor, RelativeSource={RelativeSource FindAncestor, AncestorType=local:ScrollView}}"
59
- Text="{Binding}"
24
+ ```xml:MainWindow.xaml
60
- TextAlignment="Center" />-->
61
- </DataTemplate>
62
- </ItemsControl.ItemTemplate>
63
- </ItemsControl>
64
- </ScrollViewer>
65
- </Border>
66
- </ControlTemplate>
67
- </Setter.Value>
68
- </Setter>
69
- </Style>
70
-
71
- <Style TargetType="{x:Type local:ScrollView2}">
72
- <Setter Property="TextColor" Value="Red" />
73
- <Setter Property="BorderBrush" Value="Black" />
74
- <Setter Property="BorderThickness" Value="1" />
75
- <Setter Property="CornerRadius" Value="3" />
76
- <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
77
- <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
78
- <Setter Property="VirtualizingPanel.ScrollUnit" Value="Pixel" />
79
- <Setter Property="TextBlock.TextAlignment" Value="Center" />
80
- <Setter Property="Template">
81
- <Setter.Value>
82
- <ControlTemplate TargetType="local:ScrollView2">
83
- <Border
84
- BorderBrush="{TemplateBinding BorderBrush}"
85
- BorderThickness="{TemplateBinding BorderThickness}"
86
- CornerRadius="{TemplateBinding CornerRadius}">
87
- <ScrollViewer Foreground="{TemplateBinding TextColor}">
88
- <ItemsPresenter />
89
- </ScrollViewer>
90
- </Border>
91
- </ControlTemplate>
92
- </Setter.Value>
93
- </Setter>
94
- </Style>
95
- </ResourceDictionary>
96
- ```
97
-
98
- ```xml
99
25
  <Window
100
26
  x:Class="Qie48og91me277i.MainWindow"
101
27
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
@@ -104,6 +30,8 @@
104
30
  Width="800"
105
31
  Height="450">
106
32
  <Window.Resources>
33
+
34
+ <!-- (ご破算にするようですが)Foregroundをいじっていいなら、Styleだけで済んでしまいます。 -->
107
35
  <Style x:Key="ScrollViewStyle" TargetType="{x:Type ItemsControl}">
108
36
  <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
109
37
  <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
@@ -126,26 +54,103 @@
126
54
  </Style>
127
55
  </Window.Resources>
128
56
 
129
- <UniformGrid Rows="1">
57
+ <UniformGrid Margin="10" Rows="1">
58
+ <GroupBox Header="TemplateBinding">
130
- <local:ScrollView Margin="10" ItemsSource="{Binding}" />
59
+ <local:ScrollView ItemsSource="{Binding}" />
131
-
132
- <local:ScrollView2
60
+ </GroupBox>
61
+
62
+ <GroupBox Header="継承">
63
+ <local:ScrollView2 ItemsSource="{Binding}" TextColor="Green" />
64
+ </GroupBox>
65
+
66
+ <GroupBox Header="Style only">
67
+ <ItemsControl
133
- Margin="10"
68
+ Foreground="Blue"
134
- CornerRadius="30,3,30,3"
135
- ItemsSource="{Binding}"
69
+ ItemsSource="{Binding}"
136
- TextColor="Green" />
137
-
138
- <ItemsControl
139
- Margin="10"
140
- Foreground="Blue"
141
- ItemsSource="{Binding}"
142
- Style="{StaticResource ScrollViewStyle}" />
70
+ Style="{StaticResource ScrollViewStyle}" />
71
+ </GroupBox>
143
72
  </UniformGrid>
144
73
  </Window>
145
74
  ```
146
75
 
76
+ ```xml:Generic.xaml
77
+ <ResourceDictionary
78
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
79
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
80
+ xmlns:local="clr-namespace:Qie48og91me277i">
81
+
82
+ <Style TargetType="{x:Type local:ScrollView}">
83
+ <Setter Property="TextColor" Value="Red" />
84
+ <Setter Property="Template">
85
+ <Setter.Value>
86
+ <ControlTemplate TargetType="{x:Type local:ScrollView}">
87
+ <Border
88
+ BorderBrush="Black"
89
+ BorderThickness="1"
90
+ CornerRadius="3">
91
+ <ScrollViewer
92
+ HorizontalScrollBarVisibility="Disabled"
93
+ VerticalScrollBarVisibility="Hidden"
94
+ VirtualizingPanel.ScrollUnit="Pixel">
95
+
96
+ <!-- とはいえ長ったらしいですし、事実上こうでいい気がします。 -->
97
+ <ItemsControl Foreground="{TemplateBinding TextColor}" ItemsSource="{TemplateBinding ItemsSource}">
98
+ <ItemsControl.Template>
99
+ <ControlTemplate TargetType="{x:Type ItemsControl}">
100
+ <ItemsPresenter />
101
+ </ControlTemplate>
102
+ </ItemsControl.Template>
103
+ <ItemsControl.ItemsPanel>
104
+ <ItemsPanelTemplate>
105
+ <StackPanel Orientation="Vertical" />
106
+ </ItemsPanelTemplate>
107
+ </ItemsControl.ItemsPanel>
108
+ <ItemsControl.ItemTemplate>
109
+ <DataTemplate>
110
+ <TextBlock Text="{Binding}" TextAlignment="Center" />
111
+
112
+ <!-- 字面通り素直に受け取ればこうなります。 -->
113
+ <!--<TextBlock
114
+ Foreground="{Binding TextColor, RelativeSource={RelativeSource FindAncestor, AncestorType=local:ScrollView}}"
115
+ Text="{Binding}"
116
+ TextAlignment="Center" />-->
117
+ </DataTemplate>
118
+ </ItemsControl.ItemTemplate>
119
+ </ItemsControl>
120
+ </ScrollViewer>
121
+ </Border>
122
+ </ControlTemplate>
123
+ </Setter.Value>
124
+ </Setter>
125
+ </Style>
126
+
127
+ <Style TargetType="{x:Type local:ScrollView2}">
128
+ <Setter Property="TextColor" Value="Red" />
129
+ <Setter Property="BorderBrush" Value="Black" />
130
+ <Setter Property="BorderThickness" Value="1" />
131
+ <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
132
+ <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
133
+ <Setter Property="VirtualizingPanel.ScrollUnit" Value="Pixel" />
134
+ <Setter Property="TextBlock.TextAlignment" Value="Center" />
135
+ <Setter Property="Template">
136
+ <Setter.Value>
137
+ <ControlTemplate TargetType="local:ScrollView2">
138
+ <Border
139
+ BorderBrush="{TemplateBinding BorderBrush}"
140
+ BorderThickness="{TemplateBinding BorderThickness}"
141
+ CornerRadius="3">
142
+ <ScrollViewer Foreground="{TemplateBinding TextColor}">
143
+ <ItemsPresenter />
144
+ </ScrollViewer>
145
+ </Border>
146
+ </ControlTemplate>
147
+ </Setter.Value>
148
+ </Setter>
149
+ </Style>
150
+ </ResourceDictionary>
151
+ ```
152
+
147
153
  ```cs
148
- using System;
149
154
  using System.Collections.Generic;
150
155
  using System.Linq;
151
156
  using System.Windows;
@@ -181,6 +186,7 @@
181
186
  }
182
187
  }
183
188
 
189
+ // 依存関係プロパティを追加したいだけなら、継承でいい気がします
184
190
  public class ScrollView2 : ItemsControl
185
191
  {
186
192
  public Brush TextColor { get => (Brush)GetValue(TextColorProperty); set => SetValue(TextColorProperty, value); }
@@ -188,17 +194,12 @@
188
194
  = DependencyProperty.Register(nameof(TextColor), typeof(Brush), typeof(ScrollView2),
189
195
  new PropertyMetadata(Brushes.Black));
190
196
 
191
- public CornerRadius CornerRadius { get => (CornerRadius)GetValue(CornerRadiusProperty); set => SetValue(CornerRadiusProperty, value); }
192
- public static readonly DependencyProperty CornerRadiusProperty
193
- = Border.CornerRadiusProperty.AddOwner(typeof(ScrollView2),
194
- new PropertyMetadata(new CornerRadius()));
195
-
196
197
  static ScrollView2()
197
198
  {
198
199
  DefaultStyleKeyProperty.OverrideMetadata(typeof(ScrollView2), new FrameworkPropertyMetadata(typeof(ScrollView2)));
199
200
  }
200
201
  }
202
+
201
203
  }
202
204
  ```
203
-
204
- ![アプリ画像](https://ddjkaamml8q8x.cloudfront.net/questions/2022-07-29/e921cc65-2627-4fb4-a520-ca233470c645.png)
205
+ ![アプリ画像](https://ddjkaamml8q8x.cloudfront.net/questions/2023-08-16/1be7b9f2-753b-435c-99d7-02b041631333.png)