ItemsControlのItemTemplateまでカスタムコントロールのプロパティ(TextColor)を届けたい。
字面通り素直に受け取ればこうなります。
xml
1 < DataTemplate >
2 < TextBlock
3 Foreground = " {Binding TextColor, RelativeSource={RelativeSource FindAncestor, AncestorType=local:ScrollView}} "
4 Text = " {Binding} "
5 TextAlignment = " Center " />
6 </ DataTemplate >
とはいえ長ったらしいですし、事実上こうでいい気がします(下記画像左)
xml
1 < ItemsControl Foreground = " {TemplateBinding TextColor} " ItemsSource = " {TemplateBinding ItemsSource} " >
依存関係プロパティを追加したいだけなら、継承でいい気がします(下記画像中央)
(ご破算にするようですが)Foreground
をいじっていいなら、Style
だけで済んでしまいます(下記画像右)
xml:MainWindow.xaml
1 < Window
2 x: Class = " Qie48og91me277i.MainWindow "
3 xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
4 xmlns: x = " http://schemas.microsoft.com/winfx/2006/xaml "
5 xmlns: local = " clr-namespace:Qie48og91me277i "
6 Width = " 800 "
7 Height = " 450 " >
8 < Window.Resources >
9
10 <!-- (ご破算にするようですが)Foregroundをいじっていいなら、Styleだけで済んでしまいます。 -->
11 < Style x: Key = " ScrollViewStyle " TargetType = " {x:Type ItemsControl} " >
12 < Setter Property = " ScrollViewer.HorizontalScrollBarVisibility " Value = " Disabled " />
13 < Setter Property = " ScrollViewer.VerticalScrollBarVisibility " Value = " Hidden " />
14 < Setter Property = " VirtualizingPanel.ScrollUnit " Value = " Pixel " />
15 < Setter Property = " TextBlock.TextAlignment " Value = " Center " />
16 < Setter Property = " Template " >
17 < Setter.Value >
18 < ControlTemplate TargetType = " ItemsControl " >
19 < Border
20 BorderBrush = " Black "
21 BorderThickness = " 1 "
22 CornerRadius = " 3 " >
23 < ScrollViewer >
24 < ItemsPresenter />
25 </ ScrollViewer >
26 </ Border >
27 </ ControlTemplate >
28 </ Setter.Value >
29 </ Setter >
30 </ Style >
31 </ Window.Resources >
32
33 < UniformGrid Margin = " 10 " Rows = " 1 " >
34 < GroupBox Header = " TemplateBinding " >
35 < local: ScrollView ItemsSource = " {Binding} " />
36 </ GroupBox >
37
38 < GroupBox Header = " 継承 " >
39 < local: ScrollView2 ItemsSource = " {Binding} " TextColor = " Green " />
40 </ GroupBox >
41
42 < GroupBox Header = " Style only " >
43 < ItemsControl
44 Foreground = " Blue "
45 ItemsSource = " {Binding} "
46 Style = " {StaticResource ScrollViewStyle} " />
47 </ GroupBox >
48 </ UniformGrid >
49 </ Window >
xml:Generic.xaml
1 < ResourceDictionary
2 xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
3 xmlns: x = " http://schemas.microsoft.com/winfx/2006/xaml "
4 xmlns: local = " clr-namespace:Qie48og91me277i " >
5
6 < Style TargetType = " {x:Type local:ScrollView} " >
7 < Setter Property = " TextColor " Value = " Red " />
8 < Setter Property = " Template " >
9 < Setter.Value >
10 < ControlTemplate TargetType = " {x:Type local:ScrollView} " >
11 < Border
12 BorderBrush = " Black "
13 BorderThickness = " 1 "
14 CornerRadius = " 3 " >
15 < ScrollViewer
16 HorizontalScrollBarVisibility = " Disabled "
17 VerticalScrollBarVisibility = " Hidden "
18 VirtualizingPanel.ScrollUnit = " Pixel " >
19
20 <!-- とはいえ長ったらしいですし、事実上こうでいい気がします。 -->
21 < ItemsControl Foreground = " {TemplateBinding TextColor} " ItemsSource = " {TemplateBinding ItemsSource} " >
22 < ItemsControl.Template >
23 < ControlTemplate TargetType = " {x:Type ItemsControl} " >
24 < ItemsPresenter />
25 </ ControlTemplate >
26 </ ItemsControl.Template >
27 < ItemsControl.ItemsPanel >
28 < ItemsPanelTemplate >
29 < StackPanel Orientation = " Vertical " />
30 </ ItemsPanelTemplate >
31 </ ItemsControl.ItemsPanel >
32 < ItemsControl.ItemTemplate >
33 < DataTemplate >
34 < TextBlock Text = " {Binding} " TextAlignment = " Center " />
35
36 <!-- 字面通り素直に受け取ればこうなります。 -->
37 <!--<TextBlock
38 Foreground="{Binding TextColor, RelativeSource={RelativeSource FindAncestor, AncestorType=local:ScrollView}}"
39 Text="{Binding}"
40 TextAlignment="Center" />-->
41 </ DataTemplate >
42 </ ItemsControl.ItemTemplate >
43 </ ItemsControl >
44 </ ScrollViewer >
45 </ Border >
46 </ ControlTemplate >
47 </ Setter.Value >
48 </ Setter >
49 </ Style >
50
51 < Style TargetType = " {x:Type local:ScrollView2} " >
52 < Setter Property = " TextColor " Value = " Red " />
53 < Setter Property = " BorderBrush " Value = " Black " />
54 < Setter Property = " BorderThickness " Value = " 1 " />
55 < Setter Property = " ScrollViewer.HorizontalScrollBarVisibility " Value = " Disabled " />
56 < Setter Property = " ScrollViewer.VerticalScrollBarVisibility " Value = " Hidden " />
57 < Setter Property = " VirtualizingPanel.ScrollUnit " Value = " Pixel " />
58 < Setter Property = " TextBlock.TextAlignment " Value = " Center " />
59 < Setter Property = " Template " >
60 < Setter.Value >
61 < ControlTemplate TargetType = " local:ScrollView2 " >
62 < Border
63 BorderBrush = " {TemplateBinding BorderBrush} "
64 BorderThickness = " {TemplateBinding BorderThickness} "
65 CornerRadius = " 3 " >
66 < ScrollViewer Foreground = " {TemplateBinding TextColor} " >
67 < ItemsPresenter />
68 </ ScrollViewer >
69 </ Border >
70 </ ControlTemplate >
71 </ Setter.Value >
72 </ Setter >
73 </ Style >
74 </ ResourceDictionary >
cs
1 using System . Collections . Generic ;
2 using System . Linq ;
3 using System . Windows ;
4 using System . Windows . Controls ;
5 using System . Windows . Media ;
6
7 namespace Qie48og91me277i
8 {
9 public partial class MainWindow : Window
10 {
11 public MainWindow ( )
12 {
13 InitializeComponent ( ) ;
14 DataContext = Enumerable . Range ( 1 , 100 ) . Select ( x => $"Item { x } " ) . ToList ( ) ;
15 }
16 }
17
18 public class ScrollView : Control
19 {
20 public IEnumerable < string > ItemsSource { get => ( IEnumerable < string > ) GetValue ( ItemsSourceProperty ) ; set => SetValue ( ItemsSourceProperty , value ) ; }
21 public static readonly DependencyProperty ItemsSourceProperty
22 = DependencyProperty . Register ( nameof ( ItemsSource ) , typeof ( IEnumerable < string > ) , typeof ( ScrollView ) ,
23 new PropertyMetadata ( ) ) ;
24
25 public Brush TextColor { get => ( Brush ) GetValue ( TextColorProperty ) ; set => SetValue ( TextColorProperty , value ) ; }
26 public static readonly DependencyProperty TextColorProperty
27 = DependencyProperty . Register ( nameof ( TextColor ) , typeof ( Brush ) , typeof ( ScrollView ) ,
28 new PropertyMetadata ( Brushes . Black ) ) ;
29
30 static ScrollView ( )
31 {
32 DefaultStyleKeyProperty . OverrideMetadata ( typeof ( ScrollView ) , new FrameworkPropertyMetadata ( typeof ( ScrollView ) ) ) ;
33 }
34 }
35
36 // 依存関係プロパティを追加したいだけなら、継承でいい気がします
37 public class ScrollView2 : ItemsControl
38 {
39 public Brush TextColor { get => ( Brush ) GetValue ( TextColorProperty ) ; set => SetValue ( TextColorProperty , value ) ; }
40 public static readonly DependencyProperty TextColorProperty
41 = DependencyProperty . Register ( nameof ( TextColor ) , typeof ( Brush ) , typeof ( ScrollView2 ) ,
42 new PropertyMetadata ( Brushes . Black ) ) ;
43
44 static ScrollView2 ( )
45 {
46 DefaultStyleKeyProperty . OverrideMetadata ( typeof ( ScrollView2 ) , new FrameworkPropertyMetadata ( typeof ( ScrollView2 ) ) ) ;
47 }
48 }
49
50 }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/01 01:01