将字段的值传递给Silverlight ConverterParameter [英] Pass value of a field to Silverlight ConverterParameter

查看:122
本文介绍了将字段的值传递给Silverlight ConverterParameter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写我的第一个Silverlight应用程序。我有一个datagrid的列有两个标签,对于标签,我正在使用IValueConverter有条件地格式化数据。



标签的内容设置为:

 内容={Binding HomeScore,Converter = {StaticResource fmtshs}}



$ b

  Content ={Binding AwayScore,Converter = {StaticResource fmtshs}}

我的IValueConverter的转换方法是这样的:

  public function Convert(
ByVal value As Object,
ByVal targetType As System.Type,
ByVal参数As Object,
ByVal culture As System.Globalization.CultureInfo)As Object
实现System.Windows.Data.IValueConverter.Convert

Dim score As Long = value,other As Long = parameter

返回If(score < ,
如果(得分 - 其他> 5,(其他+ 5).ToString,score.ToString)


结束函数

所以我想做的是在HomeScore的转换器中,我想通过AwayScore转换为ConverterParameter,对于AwayScore,我想将HomeScore传递给转换器。在任何得分的转换器中,我需要能够知道其他分数的值用于格式化。



但是我无法找到将ConverterParameter绑定到另一个字段的语法。

我尝试过以下操作:

  Content ={Binding HomeScore,Converter = {StaticResource fmtshs},ConverterParameter = AwayScore}
Content ={Binding HomeScore,Converter = {StaticResource fmtshs},ConverterParameter = {AwayScore}}
Content ={Binding HomeScore,Converter = {StaticResource fmtshs},ConverterParameter = {Binding AwayScore}}
/ pre>

但没有一个似乎工作。我如何将一个字段值传递给ConverterParameter?

解决方案

因为你不能将任何东西传递给 ConverterParameter 解决方案是将整个对象传递给转换器,然后您可以从转换器中访问其所有属性。



所以你的代码变成(假设你的对象称为匹配):

  public function Convert(
ByVal value As Object,
ByVal targetType As System.Type,
ByVal参数As Object,
ByVal culture As System.Globalization.CultureInfo)As Object
实现System.Windows.Data.IValueConverter.Convert

Dim match As Match = value

'使用匹配'

结束功能

(对代码中缺乏细节表示歉意)



然后你的XAML成为

  Content ={Binding Converter = {StaticResou rce fmtshs}}

注意虽然你显然是直接绑定到转换器,实际上并不是这样。您不需要指定 Path 即可绑定到数据上下文,因此您可以使用访问整个事物。



来源


I'm writing my very first Silverlight app. I have a datagrid with a column that has two labels, for the labels, i am using an IValueConverter to conditionally format the data.

The label's "Content" is set as such:

Content="{Binding HomeScore, Converter={StaticResource fmtshs}}"

and

Content="{Binding AwayScore, Converter={StaticResource fmtshs}}"

The Convert method of my IValueConverter is such:

Public Function Convert(
  ByVal value As Object, 
  ByVal targetType As System.Type, 
  ByVal parameter As Object, 
  ByVal culture As System.Globalization.CultureInfo) As Object 
Implements System.Windows.Data.IValueConverter.Convert

    Dim score As Long = value, other As Long = parameter

    Return If(score < 0, "", 
        If(score - other > 5, (other + 5).ToString, score.ToString)
    )

End Function

So what i want to do is in the converter for HomeScore, i want to pass AwayScore to the ConverterParameter, and for AwayScore i want to pass the HomeScore to the converter. In the converter for either score i need to be able to know the value of the other score for formatting purposes.

But i cannot figure out the syntax for binding the ConverterParameter to another field.
I've tried the following:

Content="{Binding HomeScore, Converter={StaticResource fmtshs}, ConverterParameter=AwayScore}"  
Content="{Binding HomeScore, Converter={StaticResource fmtshs}, ConverterParameter={AwayScore}}"  
Content="{Binding HomeScore, Converter={StaticResource fmtshs}, ConverterParameter={Binding AwayScore}}"  

But none of those seem to work. How do i pass a field value to the ConverterParameter?

解决方案

As you can't pass anything but a literal into the ConverterParameter the solution is to pass the whole object into the converter and then you can access all of it's properties from within the Converter.

So your code becomes (assuming your object is called Match):

Public Function Convert(
  ByVal value As Object, 
  ByVal targetType As System.Type, 
  ByVal parameter As Object, 
  ByVal culture As System.Globalization.CultureInfo) As Object 
Implements System.Windows.Data.IValueConverter.Convert

    Dim match As Match = value

    ' Do stuff with match'

End Function

(Apologies for lack of detail in the code)

Then your XAML becomes

Content="{Binding Converter={StaticResource fmtshs}}"

NOTE While you are apparently binding directly to the converter, that's not actually the case. You are binding to the data context without specifying a Path so you can use access the whole thing.

Source

这篇关于将字段的值传递给Silverlight ConverterParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆