2017-05-22 41 views
0

我有一個列表視圖,其中item item設置爲child。我想將一個子對象綁定到一個視圖,該視圖將通過轉換器設置顏色。Xamarin格式:IValueConverter在Convert方法中接收空值

轉換器方法被調用,但我通過的值是null

除了點,我也使用Path=/但傳遞給轉換器的值仍然是null。如果我綁定屬性,這很好,但不是當前項目。

<ListView x:Name="childListView" 
    VerticalOptions="FillAndExpand" 
    HasUnevenRows="true" 
    ItemSelected="OnItemSelected" 
    ItemTapped="OnItemTapped"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <ViewCell> 
       <ViewCell.View> 
        <StackLayout 
         BackgroundColor="{Binding ., Converter={StaticResource accountedToColorConverter}}" 
         Spacing="0" Padding="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> 
         <StackLayout Orientation="Horizontal" Spacing="10" Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> 
          <StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand"> 
           <controls:CircleImage> 
+0

什麼是「。」在你的代碼中''BackgroundColor'屬性的綁定?如果顯示此XAML的代碼,則可能更易於找出 – apineda

+0

在XAML中,值轉換器通常在「ResourceDictionary」中實例化,然後使用「StaticResource」標記擴展在Binding表達式中引用。 PLZ提供相關代碼以獲得幫助。 – Fahadsk

+0

「。」我用的是其他代碼,我在列表中有一個組綁定,在該組中,我有字符串和其他組對象。 「。」代表列表中的當前項目。我在模板選擇器中使用它,而不是在IValueConverter上使用它。 – LittleFunny

回答

1

Phatye肯定是正確地說行

BackgroundColor="{Binding ., Converter={StaticResource accountedToColorConverter}}"

是罪魁禍首。我也試圖在過去只使用{Binding .}{Binding Path=.}以及轉換器來運行您遇到的相同的空引用問題。看來Xamarin不喜歡這樣。

的妥善解決將是通過你要綁定屬性的正確路徑:

假設屬性是一個頂級物業

BackgroundColor="{Binding Path=accounted, Converter={StaticResource accountedToColorConverter}}"

否則,你可以這樣做:

BackgroundColor="{Binding Path=topLevelProperty.accounted, Converter={StaticResource accountedToColorConverter}}"

0
BackgroundColor="{Binding ., Converter={StaticResource accountedToColorConverter}}" 

這條線是有可能是罪魁禍首。它只有在頁面的綁定上下文是單個「AccountedTo」屬性時纔有效。將其更改爲"{Binding BackgroundProperty}" 其中「BackgroundProperty」是「AccountedTo」值。