IValueConverter作为对象返回的不是期望的值

问题描述 投票:0回答:1

我正在尝试将标签(或其他任何东西)上的字符串转换为另一个字符串。这是用于图标字体。

转换器

  [ValueConversion(typeof(string), typeof(string))]
    public class StringToIconConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string property = ((string)value).ToLower();
            switch (property)
            {
                case "maximize": return @"\e901";
                default return property;
            }
        }
         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }

      ...

   }

然后我将其添加到需要它的页面中

        <Helpers:StringToIconConverter x:Key="Icon" />

这就是我的设置方式

<Button Style="{StaticResource NavButton}"
                        Tag="Document"
                        Command="{Binding GotoDataMatrixEnterCommand}">
    <Button.Content>
        <Label Content="{Binding Source={RelativeSource Self}, 
                         Path=Tag,
                         Converter={StaticResource Icon}, 
                         UpdateSourceTrigger=LostFocus}" 
                         Tag="Document" />
    </Button.Content>
</Button>

我得到的是

Windows.System.Control.Label代替它。我不能正确转换它吗,我不确定为什么它不能替换正确的信息。我已经尝试了一些方法,并且尽管目前仍处于停滞状态,但仍可以在线阅读。另外,当我尝试调试时不起作用(它永远不会达到断点)

c# wpf ivalueconverter
1个回答
0
投票

问题出在Label.Content上。您应该使用RelativeSource,而不是Source。

应该是:

RelativeSource={RelativeSource Mode=Self}

[此外,您在switchStringToIconConverter语句中存在语法错误:default return property;应该为default: return property;(您缺少冒号)。

© www.soinside.com 2019 - 2024. All rights reserved.