不绑定图像源的值转换器 - 转换
我将视图模型类中的字符串属性绑定到视图中的图像源上。字符串属性对于男性可以为 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;
我看到该值是字符串类型。
我做错了什么?
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" Converter="{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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您很困惑 Binding 的不同部分与 Convert 方法参数的关系。以下是 IValueConverter.Convert 的方法签名:
value
参数来自 Binding 的值,该值通常直接分配给目标属性。在您的情况下,您的 Binding 使用{Binding .}
(相当于{Binding}
),它使用当前的 DataContext 作为源,并且不指定任何路径,因此会导致DataContext 对象作为您的值(在本例中为您的视图模型类)。Binding 中设置的 ConverterParameter 在 Convert 方法中显示为
parameter
参数。这不是绑定值,需要是某种类型的固定值:字符串、x:Static 对象引用、StaticResource 等。您声明 Binding 的方式很可能被解析为字符串:“Oponent. Info.Sex”,您应该在断点处看到它作为 Convert 方法中的参数
。您在 MultiBinding 中使用的 Binding 在正确的位置使用参数。尝试使用此方法来代替您的 Source 绑定(不需要您的 Mode 和 UpdateSourceTrigger 设置):
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:
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 theparameter
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):