您好我在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>
现在我想将转换器INSIDE应用于模板体,而不是在声明中。我知道TemplateBinding不接受转换器,所以我试图像这样使用RelativeSource Templated Parent -
<TextBlock Text="{Binding helpers:PlaceHolderForProfilePic.Initials , RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource placeholderConverter}, ConverterParameter=initials}"/>
但是存在BindingPathExpression错误,因为它不接受“:”
还有其他方法可以解决这个问题吗?
答案 0 :(得分:0)
尝试使用语法:{Binding Path =(helpers:PlaceHolderForProfilePic.Initials),...}。