开发者

Value Converter Without Binding On Image Source - conversion

开发者 https://www.devze.com 2023-02-06 07:32 出处:网络
I bind string property from view model class on image source in view. String property can have value 1 for man and 2 for woman. I use also converter on binding which returns uri for image source.

I bind string property from view model class on image source in view. String property can have value 1 for man and 2 for woman. I use also converter on binding which returns uri for image source.

In view I have this:

    <Image Style="{StaticResource InfoIcon}" 
           Source="{Binding ., Mode=OneWay,UpdateSourceTrigger=PropertyChanged,
                Converter={StaticResource sexImgConverter}, 
                ConverterParameter=Oponent.Info.Sex}"/>

Converter method is here:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        const string woman = "/images/icons/chat/woman.png";
        const string man = "/images/icons/chat/man.png";

        string result = string.Empty;

        string result = int.Parse(value.ToString()) == 1 ? man : woman;

        return new Uri(result);
    }

Problem is I bind property sex (in view Oponent.Info.Sex) which is type string and parse to integer.

But if I add debuger break point on line :

string resul = int.Parse(value.ToString()) == 1 ? man : woman;

I see that value is type of my view model class.

I try use this convert method no another bind, here is it:

<TextBlock Style="{StaticResource InfoText}">
     <TextBlock.Text>
        <MultiBinding StringFormat="{}{0}, {1} rokov">
            <Binding Path="Oponent.Info.Sex" C开发者_如何学编程onverter="{StaticResource sexConverter}"/>
            <Binding Path= "Oponent.Info.Age"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

I use the same conveter add debuger break point on line:

string resul = int.Parse(value.ToString()) == 1 ? man : woman;

I see the value is type of string.

What have I done wrong?


It looks like you're confusing how the different parts of the Binding relate to the Convert method parameters. Here's the method signature for IValueConverter.Convert:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

The value parameter comes from the Binding's value, which is what normally gets assigned directly to the target property. In your case your Binding is using {Binding .} (equivalent to just {Binding}) which uses the current DataContext as the source, and specifies no path so results in the DataContext object as your value (in this case your View Model class).

The ConverterParameter set in the Binding shows up as the parameter parameter in the Convert method. This is not a bound value and needs to be some type of fixed value: a string, x:Static object reference, StaticResource, etc. The way you've declared your Binding it is most likely being parsed as a string: "Oponent.Info.Sex", which you should see at the breakpoint as the parameter in the Convert method.

The Binding you're using in the MultiBinding is using the parameters in the correct places. Try this instead for your Source binding (your Mode and UpdateSourceTrigger settings are unneeded):

Source="{Binding Path=Oponent.Info.Sex, Converter={StaticResource sexImgConverter}}"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号