【问题标题】:WPF TemplateBinding Concatenate String for ValueWPF TemplateBinding 为值连接字符串
【发布时间】:2016-04-12 23:36:32
【问题描述】:

我想向我的自定义切换按钮模板添加自定义属性(通过依赖属性)。 现在我想让包含标签的值(作为未来实现的占位符)成为一个连接值,比如“你好”和实际的属性值。

没有串联它可以正常工作(在标签上显示“战士”)

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

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

我怎样才能做到这一点?

代码的其余部分:

我的 .xaml

<Window.Resources>
    <ResourceDictionary>
        <Style x:Key="HeroClassCheckbox" TargetType="ToggleButton">
            <Setter Property="Content" Value="Green" />
            <Setter Property="local:HeroClassCheckbox.HeroClass" Value="NoClass"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Label Content="{TemplateBinding local:HeroClassCheckbox.HeroClass}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <ToggleButton Width="150" Height="50" local:HeroClassCheckbox.HeroClass="Warrior" Style="{DynamicResource HeroClassCheckbox}"/>
</Grid>

我的 .xaml.cs

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);
    }
}

【问题讨论】:

    标签: c# wpf xaml controltemplate templatebinding


    【解决方案1】:

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

        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”在命名空间“clr-namespace:WpfApplication1”中不存在。WpfApplication1”这很奇怪,因为它显然确实存在。我在这里做错了什么? .xaml.xaml.cs
    • 当我们看不到项目文件的结构时,很难找出这个问题。寻找有关如何使用价值转换器的教程,看看是否有帮助
    • 这只是一个标准项目(新项目->wpf项目),但我会寻找价值转换器的教程。
    • @JanOechsler 你确定 HelloLabelConverter 在命名空间 WpfApplication1 内吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 2021-01-29
    相关资源
    最近更新 更多