【问题标题】:Pass value of a field to Silverlight ConverterParameter将字段的值传递给 Silverlight ConverterParameter
【发布时间】:2009-08-28 05:24:15
【问题描述】:

我正在编写我的第一个 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?

【问题讨论】:

  • 我试过单引号,但在转换器中它给了我一个字符串'{Binding SomeOtherProperty}'。我错过了什么吗? :( PS。我正在使用 Silverlight 3。
  • 查看多绑定转换器stackoverflow.com/questions/377841/…

标签: datagrid silverlight-3.0 ivalueconverter


【解决方案1】:

由于您只能将文字传递给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}}"

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

Source

【讨论】:

  • 这个问题是,如果对象上的属性被更新,那么 valueconverter 将不会触发
  • 为了清楚起见:@ChrisF,正如您在注释中提到的那样,您没有绑定到转换器。您正在使用给定的转换器绑定到当前数据上下文(如 Path = .)。绑定转换器是无稽之谈。
  • @Dercsár - 确实如此。这是简写​​,回想起来令人困惑。
【解决方案2】:

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

【讨论】:

  • 是的,这正是我想要做的。
  • 如果您还没有弄清楚,单引号就是您的问题代码中缺少的内容。
【解决方案3】:

我也遇到了同样的问题,不得不睡着了。似乎转换器通过 Binding 值“一次性”获取数据。

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

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

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

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

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

然后删除 .txt

【讨论】:

  • 我认为这是迄今为止最好的方法。它将复杂性的责任委托给它所属的对象 +1
【解决方案4】:

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();
    }
}

【讨论】:

    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        猜你喜欢
        • 2012-03-21
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 2018-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-24
        相关资源
        最近更新 更多