2009-08-28 66 views
12

我正在編寫我的第一個Silverlight應用程序。我有一個具有兩個標籤的列的datagrid,對於標籤,我使用IValueConverter來有條件地格式化數據。將字段的值傳遞給Silverlight ConverterParameter

標籤的 「內容」 被設定爲這樣:

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

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

轉換我的IValueConverter的方法是這樣的:

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?

+0

我曾嘗試單是有用的引號,但在轉換器中,它給了我一串'{綁定SomeOtherProperty}'。我錯過了什麼嗎? :(PS。我使用Silverlight 3 – 2010-07-16 01:31:12

+0

看多路捆綁轉換器 http://stackoverflow.com/questions/377841/what-should-the-converter-parameter-be-for-this-binding – Rauld 2012-08-08 09:27:28

回答

6

貌似你試圖綁定到ConverterParameter這恐怕不能。 ConverterParameter只能採用文字值,例如ConverterParameter ='您的字符串'

+0

是,這正是我當時想要做的事情 – eidylon 2009-08-28 14:56:14

+3

如果你還沒有弄明白,那麼單引號就是你在問題代碼中缺少的內容 – Aligned 2010-04-21 16:00:46

2

我有同樣的問題,不得不睡在它上面。似乎轉換器在獲取數據方面「一炮打響」 - 通過綁定值。

因此,使綁定值成爲一個複雜的類。如果你正在使用M-V-VM,你應該是數據整形,所以我通過在我的轉換器中包含顯示值和其他需要的數據(如果你願意的話創建一​​個包含的類)來使Binding值'工作更加努力'。

接下來,我需要讓Converter'工作更加努力',它將ConverterParameters作爲Value類型文字傳遞的限制,所以我在我的轉換器中創建了一個Enum,並將其轉換爲更多優雅的文字。

然後我可以做的是基於顯示的值和另一個閾值(我檢查),根據網格單元的顏色(刷子)和厚度的不同而變化。

的源代碼是在我的博客網站,使用僞MV-VM的方式結合了Silverlight 3的代碼(不依賴注入,但嘿 - 它的一個例子,是吧?)

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

然後取出.TXT

+0

我認爲這是迄今爲止最好的方法。將複雜度的責任委託給它所屬的對象+1 – 2011-01-17 18:19:54

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}}" 

注意雖然你顯然直接綁定到轉換器,但事實並非如此。你綁定到數據上下文而沒有指定Path,所以你可以使用訪問整個事情。

Source

+6

但是,這個問題的問題是,如果對象上的屬性被更新,那麼valueconverter將不會觸發 – Calanus 2011-03-25 10:40:53

+0

只是爲了清楚起見:@ChrisF,您沒有約束轉換器,就像你在註釋中提到的那樣,你綁定了當前的datacontext(就像Path =。),使用給定的轉換器。綁定到轉換器是無稽之談。 – 2015-10-01 09:39:38

+0

@Dercsár - 的確如此。這是簡短的回顧性混淆。 – ChrisF 2015-10-01 09:52:05

2

ChrisF有我已經能夠得出唯一的解決辦法 - 綁定整個數據對象的內容屬性和使用內置A轉換器期望此對象類型解析您在轉換器本身需要的屬性。

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