質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
.NET

.NETとは、主に.NET Frameworkと呼ばれるアプリケーションまたは開発環境を指します。CLR(共通言語ランタイム)を搭載し、入力された言語をCIL(共通中間言語)に変換・実行することが可能です。そのため、C#やPythonなど複数の言語を用いることができます。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

1回答

972閲覧

カスタムコントロールのテンプレート内のテンプレートにプロパティを届けたい

yamaj

総合スコア1

.NET

.NETとは、主に.NET Frameworkと呼ばれるアプリケーションまたは開発環境を指します。CLR(共通言語ランタイム)を搭載し、入力された言語をCIL(共通中間言語)に変換・実行することが可能です。そのため、C#やPythonなど複数の言語を用いることができます。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

1クリップ

投稿2022/07/28 23:44

前提

.NET6のWPFでカスタムコントロールを作りたいと思い、試行錯誤しています。
スタイルやテンプレートの理解が不足しているせいだとは思うのですが、カスタムコントロールのプロパティを、カスタムコントロール内に配置したItemsControlのItemTemplateまで渡す方法がわかりません。
どういった方法で実現させれば良いでしょうか。

やりたいこと

下記ソースにて、ItemsControlのItemTemplateまでカスタムコントロールのプロパティ(TextColor)を届けたい。

C#

1 <Style TargetType="{x:Type local:ScrollView}"> 2 <Setter Property="TextColor" Value="Red"/> <!-- これを渡したい --> 3 4 <Setter Property="Template"> 5 <Setter.Value> 6 <ControlTemplate TargetType="{x:Type local:ScrollView}"> 7 <Border BorderBrush="Black" BorderThickness="1" CornerRadius="3"> 8 <ScrollViewer HorizontalScrollBarVisibility="Disabled" 9 VerticalScrollBarVisibility="Hidden" 10 VirtualizingPanel.ScrollUnit="Pixel"> 11 <ItemsControl ItemsSource="{TemplateBinding ItemsSource}"> 12 <ItemsControl.Template> 13 <ControlTemplate TargetType="{x:Type ItemsControl}"> 14 <ItemsPresenter/> 15 </ControlTemplate> 16 </ItemsControl.Template> 17 <ItemsControl.ItemsPanel> 18 <ItemsPanelTemplate> 19 <StackPanel Orientation="Vertical" /> 20 </ItemsPanelTemplate> 21 </ItemsControl.ItemsPanel> 22 <ItemsControl.ItemTemplate> 23 <DataTemplate> <!-- ここの色を上で設定したい --> 24 <TextBlock Text="{Binding}"Foreground="{Binding TextColor}" TextAlignment="Center"/> 25 </DataTemplate> 26 </ItemsControl.ItemTemplate> 27 </ItemsControl> 28 </ScrollViewer> 29 </Border> 30 </ControlTemplate> 31 </Setter.Value> 32 </Setter> 33 </Style>

C#

1public class ScrollView : Control 2{ 3 /////////////////////////////////// 4 #region // 追加プロパティ 5 /////////////////////////////////// 6 7 /// <summary> 8 /// アイテムソースの登録 9 /// </summary> 10 public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable<string>), typeof(ScrollView), new PropertyMetadata()); 11 /// <summary> 12 /// アイテムソース取得・設定 13 /// </summary> 14 public IEnumerable<string> ItemsSource 15 { 16 get { return (IEnumerable<string>)GetValue(ItemsSourceProperty); } 17 set { SetValue(ItemsSourceProperty, value); } 18 } 19 20 /// <summary> 21 /// テキスト色の登録 22 /// </summary> 23 public static readonly DependencyProperty TextColorProperty = DependencyProperty.Register("TextColor", typeof(Brush), typeof(ScrollView), new PropertyMetadata(Brushes.Black)); 24 /// <summary> 25 /// テキスト色取得・設定 26 /// </summary> 27 public Brush TextColor 28 { 29 get { return (Brush)GetValue(TextColorProperty); } 30 set { SetValue(TextColorProperty, value); } 31 } 32 #endregion 33 /////////////////////////////////// 34 35 static ScrollView() 36 { 37 DefaultStyleKeyProperty.OverrideMetadata(typeof(ScrollView), new FrameworkPropertyMetadata(typeof(ScrollView))); 38 39 } 40}

補足情報(FW/ツールのバージョンなど)

Visual Studio 2022
WPF

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

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

1using System.Collections.Generic; 2using System.Linq; 3using System.Windows; 4using System.Windows.Controls; 5using System.Windows.Media; 6 7namespace 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/07/29 10:10

編集2023/08/16 05:36
TN8001

総合スコア9321

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yamaj

2022/08/01 01:01

ご回答ありがとうございます。 眺めているだけでは理解が追いつかないので、一つ一つ試してみます。 理解できた時点で評価させてください。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問