2

我知道我們可以直接綁定Xaml中的屬性。 但我的要求有點不同。我想完全控制綁定數據。 所以我正在尋找適配器類型的方法。我想根據該項目的文本塊中的行數顯示一些元素。在這裏,我不能使用值轉換器,因爲那時我的UI將不會準備好,我無法找到每個文本塊的行數。什麼是Windows Phone 8.1(Xaml)中android的listview適配器相當於

+0

https://books.google.lk/books?id=y_PM40ZegSQC&pg=PA246&lpg=PA246&dq=what+is+equivalent+ +列表視圖+在+窗口+安卓+適配器+的+電話與源= BL&OTS = 2eNz_ZUfdF與SIG = MKE59E5RHsqlTLypormf3ZkB5z0&HL = EN&SA = X&VED = 0ahUKEwiB5sCExoPKAhWBA44KHQWkBdoQ6AEIMTAE#v = onepage&q =什麼%圖20是%20equivalent%20of%20listview%20adapter%20of%20android%20英寸% 20windows%20phone&F =假 – Codeone

回答

-1

我會用例子來解釋一下,假設我想在綁定過程中將時間格式從hh:mm:ss更改爲hh:mm。

首先,我將創建實現IValueConverter的公共類。

例如: -

public class RemoveSecondsInTime : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     string date = value as string; 

     date = date.Substring(0, date.Length - 3); 

     return date; 
    } 

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

其次,要使用該轉換器類,我們需要在頁面資源添加該轉換器類。

例如: -

<Page.Resources> 
    <local:RemoveSecondsInTime x:Key="ChangeTimeFormat" /> 
</Page.Resources> 

第三,我們將創造創造我們的ListView如下: -

<ListView> 
    <ListView.ItemTemplate> 
     <DataTemplate > 
      <TextBlock Text="{Binding TimeWithSeconds, Converter {StaticResource ChangeTimeFormat}}" />  
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

所以,TimeWithSeconds將通過在轉換功能 「價值」 的參數,轉換功能將返回格式化的字符串以顯示在文本框中。

參考文獻: -

1)https://channel9.msdn.com/Series/Windows-Phone-8-1-Development-for-Absolute-Beginners/Part-25-Advanced-Binding-with-Value-Converters

2)https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.data.ivalueconverter