【问题标题】:Using Converters with TemplateBinding or Relativesource Templated Parent将转换器与 TemplateBinding 或 Relativesource Templated Parent 一起使用
【发布时间】:2015-12-26 07:17:21
【问题描述】:

您好,我在 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 错误,因为它不接受“:”

还有其他方法可以解决这个问题吗?

【问题讨论】:

    标签: xaml windows-phone templatebinding


    【解决方案1】:

    尝试使用语法:{Binding Path=(helpers:PlaceHolderForProfilePic.Initials), ...}。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      • 2012-02-04
      • 2013-10-17
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 2014-01-28
      相关资源
      最近更新 更多