2017-03-27 23 views
0

我有一個Listview(綁定到ObservableCollection),所有元素都基於IValueConverter進行啓用/禁用計算。Xamarin:如何在通知調用中調用IValueConverter

下面是的IValueConverter代碼...

public class StateCheckConverter: IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      var result = false; 

      if (value != null) 
      { 
       var element = value as Element; 
       if (element.Status != Status.Pending) 
        result = true; 
      } 

      return result; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      return value; 
     } 
    } 

現在我已經收到了通知(從MessageCenter)和呼叫的要素之一的恢復狀態發生了變化。我能夠改變元素的文本和值(比如標籤,圖片使用INotifyPropertyChanged)。但是,如何調用相應的IValueConverter並更新ObservableCollection?

謝謝。

UPDATE:

<ContentPage.Resources> 
    <ResourceDictionary> 
     <vm:StateCheckConverter x:Key="transmissionStateCheck" /> 
    </ResourceDictionary> 
    </ContentPage.Resources> 

<Label x:Name="lblLocked" 
         IsVisible="{Binding ., Converter={StaticResource transmissionStateCheck}, Mode=TwoWay}" 
         HorizontalTextAlignment="Center" 
         BackgroundColor="Gray" 
         Opacity="0.75" 
         Text="LOCKED" 
         TextColor="White" 
         FontSize="35" 
         /> 
+0

你如何使用轉換器?請將代碼綁定到項目的「IsEnabled」狀態。 – Emad

+0

@Emad我已經更新了代碼.. – Xander

回答

1

好的一個辦法是改變你綁定屬性,並綁定到Status本身:

<Label x:Name="lblLocked" 
     IsVisible="{Binding Status, Converter={StaticResource transmissionStateCheck}}" 
     HorizontalTextAlignment="Center" 
     BackgroundColor="Gray" 
     Opacity="0.75" 
     Text="LOCKED" 
     TextColor="White" 
     FontSize="35"/> 

當然,你便要改變您的價值轉換器以及:

public class StateCheckConverter: IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var result = false; 

     if (value is Status status) 
     { 
      if (status != Status.Pending) 
       result = true; 
     } 

     return result; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

我希望它有幫助:)

此外,我已經改變了你的代碼中的一些東西。你不能綁定IsVisible雙向模式,所以它會自動成爲一種方式。

也轉換回去應該採取bool並返回Status這是不可能和不必要的,所以我刪除它。

+1

完美地工作,+1爲您挖掘的方式:) – Xander

相關問題