1

我有以下XAML代碼,其中我將一些數據綁定到列表視圖。另外,我有非常重要的RecipientConverter,它允許我轉換我的TextBlock。但是,這是問題。該TextBlock必須按照下面TextBlock中提供的格式(Binding Path = Sum)進行轉換。 所以,這是我的問題,是否可以將「Sum」TextBlock作爲ConverterParameter發送到RecipientConverter?我知道MultipleBinding,但這隻適用於WPF,並不適用於UWP。也許這是一種與DependencyProperty一起實現它的方式,但我很確定。使用ConverterParameter的UWP多重綁定

NOTE: 「收件人」TextBlock和「Sum」TextBlock是我從服務器獲得的動態值。

<ListView x:Name="HistoryList" Padding="10" IsItemClickEnabled="True" Visibility="Collapsed" ItemsSource="{Binding Source={StaticResource TransactionsCVS}}" ItemsPanel="{StaticResource ResourceKey=ItemsPanelTemplate}" ItemClick="HistoryList_ItemClick"> 
        <ListView.GroupStyle> 
         <GroupStyle> 
          <GroupStyle.HeaderTemplate> 
           <DataTemplate x:DataType="data:TransactionGroupInfo"> 
            <TextBlock FontWeight="Medium" FontSize="16" Foreground="#999999" Text="{Binding Path=Key}" /> 
           </DataTemplate> 
          </GroupStyle.HeaderTemplate> 
         </GroupStyle> 
        </ListView.GroupStyle> 
        <ListView.ItemTemplate> 
         <DataTemplate x:DataType="data:Transaction"> 
          <Grid Height="60" Margin="0,5,0,5" Background="White" CornerRadius="5"> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="5" /> 
            <ColumnDefinition Width="*" /> 
            <ColumnDefinition Width="Auto" /> 
           </Grid.ColumnDefinitions> 
           <StackPanel Background="{Binding Path=VendorAccentColor}" CornerRadius="5,0,0,5" /> 
           <StackPanel VerticalAlignment="Center" Margin="10,0,0,0" Grid.Column="1" Orientation="Vertical"> 
            <TextBlock Text="{Binding Path=VendorName}" FontSize="16" Foreground="#999999" /> 
            <TextBlock Text="{Binding Path=Recipient, Converter={StaticResource RecipientConverter}}" FontSize="16" Foreground="#999999" /> 
           </StackPanel> 
           <TextBlock Grid.Column="2" Text="{Binding Path=Sum, Converter={StaticResource SumConverter}}" VerticalAlignment="Center" Canvas.ZIndex="2" Margin="0,0,10,0" FontSize="18" FontWeight="Bold" /> 
          </Grid> 
         </DataTemplate> 
        </ListView.ItemTemplate> 
       </ListView> 

這裏是我的變換器的C#代碼

public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     string recipient = (string)value; 
     // Here is a way I want it to be 
     string sum = (string)parameter; 

     if (HalykWallet_v03.Model.AppSettings.GetAppLang() == "ru") 
     { 
      if (sum.Contains("-")) 
       return "На " + recipient; 
      else 
       return "От " + recipient; 
     } 
     else 
     { 
      char[] array = sum.ToCharArray(); 
      if (sum.Contains("-")) 
      { 
       switch (array.Last()) 
       { 
        case '0': 
        case '1': 
        case '2': 
         recipient += recipient + "-ден"; 
         break; 
        case '3': 
        case '4': 
        case '5': 
         recipient += recipient + "-тен"; 
         break; 
        case '6': 
         recipient += recipient + "-дан"; 
         break; 
        case '7': 
        case '8': 
         recipient += recipient + "-ден"; 
         break; 
        case '9': 
         recipient += recipient + "-дан"; 
         break; 
        default: 
         break; 
       } 
      } 
      else 
      { 
       switch (array.Last()) 
       { 
        case '0': 
        case '1': 
        case '2': 
         recipient += recipient + "-ге"; 
         break; 
        case '3': 
        case '4': 
        case '5': 
         recipient += recipient + "-ке"; 
         break; 
        case '6': 
         recipient += recipient + "-ға"; 
         break; 
        case '7': 
        case '8': 
         recipient += recipient + "-ден"; 
         break; 
        case '9': 
         recipient += recipient + "-ға"; 
         break; 
       } 
      } 

      return recipient; 
     } 
    } 

回答

-1

你好有你tryed結合它作爲轉換器的參數?

<StackPanel VerticalAlignment="Center" Margin="10,0,0,0" Grid.Column="1" Orientation="Vertical"> 
    <TextBlock Text="{Binding Path=VendorName}" FontSize="16" Foreground="#999999" /> 
    <TextBlock Name="txtRecipient" Text="{Binding Path=Recipient, Converter={StaticResource RecipientConverter}}" FontSize="16" Foreground="#999999" /> 
</StackPanel> 
<TextBlock Grid.Column="2" Text="{Binding Path=Sum, Converter={StaticResource SumConverter},ConverterParameter={Binding ElementName=txtrec}}" VerticalAlignment="Center" Canvas.ZIndex="2" Margin="0,0,10,0" FontSize="18" FontWeight="Bold" /> 

但是,您可以使用Cimbalino Windows手機工具包模擬UWP中的多重綁定。看看here

+0

不,不幸的是,它不起作用,在轉換器內參數等於NULL。我在某處閱讀了ConverterParameter中不支持Binding的地方。這就是問題所在...... –

+1

您可以使用Cimbalino windows手機工具包模擬多重綁定行爲:https://www.pedrolamas.com/2013/05/17/cimbalino-windows-phone-toolkit-multibindingbehavior/ –

+0

歡迎您: ), –

1

您可以在轉換器中使用依賴屬性。 This應該有所幫助。

+0

Hello!感謝您的回覆!我閱讀你提到的主題,並使用DependencyProperty編輯我的代碼。但是,這是一個小問題。我的ListView綁定到我的ViewModel中的列表(ItemsSource設置爲List),所以我必須將我的DependencyProperty綁定到列表中的Item。我怎樣才能動態地做到這一點? –

+1

您可以使用元素名綁定將轉換器的依賴項屬性綁定到'sum'文本塊。這應該工作,否則我會建議你在你的視圖模型中有轉換邏輯。 –

+0

再次您好!如您所知,我使用了Hasan Hasanov提供的CImbalino工具包的解決方案,但這是該工具包的問題。它總是在釋放模式下引發異常。所以,我使用綁定與DependencyProperty返回到您的解決方案,但ElementName綁定不起作用)任何其他的想法? –

0

綁定而不是x:bind解決了我的問題。也許這可以節省一天的時間。 Here is the link