12

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

标签的“内容”设置如下:

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

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

我的 IValueConverter的Convert方法是这样的:

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 传递给转换器。在任一分数的转换器中,我需要能够知道另一个分数的值以进行格式化。

但我无法弄清楚将 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}}"  

但这些似乎都不起作用。如何将字段值传递给 ConverterParameter?

4

6 回答 6

7

由于您不能将除文字之外的任何内容传递到ConverterParameter解决方案中,因此将整个对象传递给转换器,然后您可以从转换器中访问它的所有属性。

所以你的代码变成了(假设你的对象被调用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

(抱歉代码中缺少细节)

然后你的 XAML 变成

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

注意虽然您显然是直接绑定到转换器,但实际上并非如此。您在没有指定 a 的情况下绑定到数据上下文,Path因此您可以使用访问整个事物。

资源

于 2010-12-08T14:19:03.143 回答
6

看起来您正在尝试绑定到 ConverterParameter ,恐怕您不能。ConverterParameter 只能采用文字值,例如 ConverterParameter='Your string'

于 2009-08-28T12:15:58.230 回答
2

ChrisF 有我能想到的唯一解决方案 - 将整个数据对象绑定到内容属性,并使用为期望此对象类型而构建的转换器解析转换器本身所需的属性。

<sdk:DataGridTextColumn Header="Report Name" Binding="{Binding Mode=OneTime, Converter={StaticResource ReportNameDateQuarterConverter}}" />


/// <summary>
/// Gets Exposure Report Name Quarter Number formatted from Report.Date and Report.Name
/// </summary>
public class ReportNameDateQuarterConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string qStr = "Quarter ";
        if (value != null && value.GetType() == typeof(Report))
        {
            switch (((Report)value).Date.Month)
            {
                case 1:
                case 2:
                case 3:
                    return qStr + "1 " + ((Report)value).Name;
                case 4: 
                case 5:
                case 6:
                    return qStr + "2 " + ((Report)value).Name;
                case 7:
                case 8:
                case 9:
                    return qStr + "3 " + ((Report)value).Name;
                case 10:
                case 11:
                case 12:
                    return qStr + "4 " + ((Report)value).Name;
                default:
                    return qStr + "? " + ((Report)value).Name;

            }
        }
        return qStr + "? " + ((Report)value).Name;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
于 2011-01-23T23:19:21.063 回答
2

我有同样的问题,不得不睡在上面。似乎转换器通过 Binding 值“一枪”获取数据。

所以让 Binding 值成为一个复杂的类。如果您使用的是 MV-VM,那么无论如何您都应该进行数据整形,因此我通过在转换器中包含显示值和我需要的其他数据(如果您愿意,创建了一个包含的类),使 Binding 值“更努力地工作”。

接下来,我需要让转换器“更努力地工作”,因为它限制将 ConverterParameters 作为值类型文字传递,因此我在转换器中创建了一个 Enum,并将文字投射到它上面,以便在我的 Convert 例程中更加优雅。

然后我可以做的是根据显示的值和另一个阈值(我检查的)改变网格单元格的颜色(画笔)和厚度。

源代码在我的博客站点上,它的 Silverlight 3 代码以伪 MV-VM 方式使用绑定(没有依赖注入,但是嘿 - 它是一个示例,对吗?)

下载地址: http ://www.martymazurik.com/file.axd?file=2010%2f6%2fHighlightGridCell.zip.txt

然后删除 .txt

于 2010-06-12T14:26:28.097 回答
0

如果你想绑定转换器参数,看看这个:http ://brandontruong.blogspot.com/2009/06/binding-for-converter-parameter.html它可能不是最干净的解决方案,但它很简单在某些情况下可能很有用

于 2011-09-26T09:09:42.213 回答
0

不完全确定我理解您的问题,但我认为您正在寻找与“元素名称”的绑定?

例如: http: //www.wintellect.com/CS/blogs/jprosise/archive/2009/03/27/silverlight-3-s-new-element-data-binding.aspx

于 2009-08-28T11:03:48.800 回答