Wpf 不绑定图像源的值转换器-转换

Wpf 不绑定图像源的值转换器-转换,wpf,image,binding,converter,Wpf,Image,Binding,Converter,我从视图中的图像源上的视图模型类绑定字符串属性。字符串属性的值可以为1(男子)和2(女子)。我还在绑定上使用转换器,它返回图像源的uri 我认为: <Image Style="{StaticResource InfoIcon}" Source="{Binding ., Mode=OneWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource sexIm

我从视图中的图像源上的视图模型类绑定字符串属性。字符串属性的值可以为1(男子)和2(女子)。我还在绑定上使用转换器,它返回图像源的uri

我认为:

    <Image Style="{StaticResource InfoIcon}" 
           Source="{Binding ., Mode=OneWay,UpdateSourceTrigger=PropertyChanged,
                Converter={StaticResource sexImgConverter}, 
                ConverterParameter=Oponent.Info.Sex}"/>
问题是我绑定了属性sex(在视图opont.Info.sex中),它是字符串类型,并解析为整数

但如果我在第行添加调试程序断点:

string resul = int.Parse(value.ToString()) == 1 ? man : woman;
我看到该值是我的视图模型类的类型。

我尝试使用此转换方法,但没有其他绑定,如下所示:

<TextBlock Style="{StaticResource InfoText}">
     <TextBlock.Text>
        <MultiBinding StringFormat="{}{0}, {1} rokov">
            <Binding Path="Oponent.Info.Sex" Converter="{StaticResource sexConverter}"/>
            <Binding Path= "Oponent.Info.Age"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>
我看到值是字符串类型


我做错了什么?

看起来您混淆了绑定的不同部分与Convert方法参数的关系。以下是IValueConverter.Convert的方法签名:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
value
参数来自绑定的值,该值通常直接分配给目标属性。在本例中,您的绑定使用的是
{Binding.}
(相当于
{Binding}
),它使用当前的DataContext作为源,并且没有指定路径,因此导致DataContext对象作为您的值(在本例中是视图模型类)

绑定中设置的ConverterParameter在Convert方法中显示为
参数
参数。这不是绑定值,需要某种类型的固定值:字符串、x:静态对象引用、StaticResource等。您声明绑定的方式很可能被解析为字符串:“Oponent.Info.Sex”,您应该在断点处将其作为Convert方法中的
参数

在多重绑定中使用的绑定在正确的位置使用参数。请在源绑定中尝试此操作(不需要模式和UpdateSourceTrigger设置):

string resul = int.Parse(value.ToString()) == 1 ? man : woman;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
Source="{Binding Path=Oponent.Info.Sex, Converter={StaticResource sexImgConverter}}"