【发布时间】:2011-06-13 16:24:54
【问题描述】:
我在视图中的图像源上绑定来自视图模型类的字符串属性。字符串属性的值可以是男人的值 1,女人的值是 2。我还在绑定上使用转换器,它返回图像源的 uri。
鉴于我有这个:
<Image Style="{StaticResource InfoIcon}"
Source="{Binding ., Mode=OneWay,UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource sexImgConverter},
ConverterParameter=Oponent.Info.Sex}"/>
转换器方法在这里:
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);
}
问题是我绑定属性sex(在Oponent.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>
我使用相同的转换器在线添加调试器断点:
string resul = int.Parse(value.ToString()) == 1 ? man : woman;
我看到值是字符串类型。
我做错了什么?
【问题讨论】:
标签: wpf image binding converter