こんにちは、ContentControl テンプレート内で添付プロパティを使用しています。これまでの私のアプローチはこれです-
添付プロパティを持つクラス
public class PlaceHolderForProfilePic : DependencyObject
{
public static readonly DependencyProperty InitialsProperty = DependencyProperty.RegisterAttached("Initials", typeof(string), typeof(PlaceHolderForProfilePic), new PropertyMetadata(""));
public static string GetInitials(DependencyObject d)
{
return (string)d.GetValue(InitialsProperty);
}
public static void SetInitials(DependencyObject d, string value)
{
d.SetValue(InitialsProperty, value);
}
}
コントロール テンプレート宣言
<ContentControl helpers:PlaceHolderForProfilePic.Initials="{Binding FullName,Converter={StaticResource placeholderConverter},ConverterParameter=initials}" Width="60" Height="60" Template="{StaticResource DefaultProfilePictureItemTemplate2}"/>
コントロール テンプレートの本文
<ControlTemplate x:Key="DefaultProfilePictureItemTemplate2">
<Grid Name="parentGrid">
<TextBlock Text="{TemplateBinding helpers:PlaceHolderForProfilePic.Initials}" />
</Grid>
</ControlTemplate>
ここで、宣言ではなく、テンプレート本体の内側にコンバーターを適用したいと思います。TemplateBinding はコンバーターを受け入れないことを知っているので、このように RelativeSource Templated Parent を使用しようとしました-
<TextBlock Text="{Binding helpers:PlaceHolderForProfilePic.Initials , RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource placeholderConverter}, ConverterParameter=initials}"/>
ただし、「:」を受け入れないため、BindingPathExpression エラーが発生します。
これを解決する他の方法はありますか?