将字段值传递给 Silverlight ConverterParameter [英] Pass value of a field to Silverlight ConverterParameter
问题描述
我正在编写我的第一个 Silverlight 应用程序.我有一个带有两个标签的列的数据网格,对于标签,我使用 IValueConverter 有条件地格式化数据.
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}}"
和
Content="{Binding AwayScore, Converter={StaticResource fmtshs}}"
我的 IValueConverter 的 Convert 方法是这样的:
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
所以我想要做的是在 HomeScore 的转换器中,我想将 AwayScore 传递给 ConverterParameter,而对于 AwayScore,我想将 HomeScore 传递给转换器.在任一分数的转换器中,我需要能够知道另一个分数的值以进行格式化.
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.
但我无法弄清楚将 ConverterParameter 绑定到另一个字段的语法.
我尝试了以下方法:
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}}"
但这些似乎都不起作用.如何将字段值传递给 ConverterParameter?
But none of those seem to work. How do i pass a field value to the ConverterParameter?
推荐答案
因为除了文字之外,你不能将任何东西传递到 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.
所以你的代码变成了(假设你的对象叫做Match
):
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)
那么你的 XAML 就变成了
Then your XAML becomes
Content="{Binding Converter={StaticResource fmtshs}}"
注意 虽然您显然是直接绑定到转换器,但实际上并非如此.您绑定到数据上下文而不指定 Path
,因此您可以访问整个内容.
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.
这篇关于将字段值传递给 Silverlight ConverterParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!