使用ConverterParameter进行UWP多重绑定

时间:2016-06-06 17:13:39

标签: xaml windows-phone-8.1 win-universal-app uwp windows-10-universal

我有以下XAML代码,其中我将一些数据绑定到listview。此外,我有非常重要的RecipientConverter,它允许我转换我的TextBlock。但是,这是问题所在。此TextBlock必须根据下面的TextBlock(Binding Path = Sum)中显示的格式进行转换。 所以,这是我的问题,是否可以将“Sum”TextBlock作为ConverterParameter发送到RecipientConverter?我知道MultipleBinding,但这只适用于WPF,并且在UWP中不可用。也许这是用DependencyProperty实现它的一种方式,但我注意到这一点。

注意: “收件人”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;
        }
    }

3 个答案:

答案 0 :(得分:1)

您可以在转换器中使用依赖项属性。This应该有所帮助。

答案 1 :(得分:0)

绑定而不是x:bind解决了我的问题。也许这会拯救某人一天。 Here is the link

答案 2 :(得分:-1)

你好试过将它绑定为转换器参数吗?

 <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