C# WPF TemplateBinding值的连接字符串

C# WPF TemplateBinding值的连接字符串,c#,wpf,xaml,controltemplate,templatebinding,C#,Wpf,Xaml,Controltemplate,Templatebinding,我想将自定义属性(通过依赖项属性)添加到我的自定义ToggleButton模板中。 现在,我想让包含标签的值(作为未来实现的占位符)成为连接的值,比如“Hello”和实际的属性值 如果没有连接,它可以正常工作(在标签上显示“Warrior”) 但是当我尝试将标签设置为串联时,xaml不再编译 <Label Content="Hello {TemplateBinding local:HeroClassCheckbox.HeroClass}"/> 您应该使用转换器作为实现目标的方法。下

我想将自定义属性(通过依赖项属性)添加到我的自定义ToggleButton模板中。 现在,我想让包含标签的值(作为未来实现的占位符)成为连接的值,比如“Hello”和实际的属性值

如果没有连接,它可以正常工作(在标签上显示“Warrior”)

但是当我尝试将标签设置为串联时,xaml不再编译

<Label Content="Hello {TemplateBinding local:HeroClassCheckbox.HeroClass}"/>

您应该使用转换器作为实现目标的方法。下面是一个例子

    public class HelloLabelConverter : IValueConverter
    {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                    char[] removeThis = "Hello ".ToCharArray();
                    return value.ToString().TrimStart(removeThis);
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                    return string.Format("Hello {0}", value);
            }
    }

   <Window.Resources>
            <local:HelloLabelConverter x:Key="HelloLabelConverter" />
   </Window.Resources>
   <Grid>
       <Label Content="{Binding ElementName= lblPropertyToBind, Path=Text, Converter={StaticResource HelloLabelConverter}}"></Label>
   </Grid>
公共类HelloLabelConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
char[]removeThis=“Hello”.ToCharArray();
返回值.ToString().TrimStart(删除此项);
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
返回string.Format(“Hello{0}”,value);
}
}

这看起来正是我想要的,但不幸的是它抛出了:“名称“HelloLabelConverter”不存在于名称空间“clr namespace:WpfApplication1.WpfApplication1”中,这很奇怪,因为它显然存在。我做错了什么?当我们看不到项目文件的结构时,很难找出这个问题。查找有关如何使用值转换器的教程,并查看它是否有帮助。这只是一个标准项目(新建项目->wpf项目),但我将查找有关值转换器的教程。@JanOechsler是否已确保HelloLabelConverter位于命名空间WpfApplication1中?
public static class HeroClassCheckbox
{
    public static readonly DependencyProperty HeroClassProperty = DependencyProperty.RegisterAttached("HeroClass",
        typeof(string), typeof(HeroClassCheckbox), new FrameworkPropertyMetadata(null));

    public static string GetHeroClass(UIElement element)
    {
        if (element == null)
            throw new ArgumentNullException("element");
        return (string)element.GetValue(HeroClassProperty);
    }
    public static void SetHeroClass(UIElement element, string value)
    {
        if (element == null)
            throw new ArgumentNullException("element");
        element.SetValue(HeroClassProperty, value);
    }
}
    public class HelloLabelConverter : IValueConverter
    {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                    char[] removeThis = "Hello ".ToCharArray();
                    return value.ToString().TrimStart(removeThis);
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                    return string.Format("Hello {0}", value);
            }
    }

   <Window.Resources>
            <local:HelloLabelConverter x:Key="HelloLabelConverter" />
   </Window.Resources>
   <Grid>
       <Label Content="{Binding ElementName= lblPropertyToBind, Path=Text, Converter={StaticResource HelloLabelConverter}}"></Label>
   </Grid>