1

I have the following XAML code, where I Bind some data to the listview. Also, I have very important RecipientConverter which allows me to convert my TextBlock. But, here is the problem. This TextBlock must be converter according to the form presented in the TextBlock below (Binding Path=Sum). So, here is my question, is it possible to send the "Sum" TextBlock as ConverterParameter to the RecipientConverter? I know about MultipleBinding, but this is only suitable for WPF and it is not available in UWP. Maybe here is a way of implementing it with DependencyProperty, but I'm note sure about that.

NOTE: "Recipient" TextBlock and "Sum" TextBlock are dynamic values which I get from the server.

<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>

And here is C# code of my Converter

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;
        }
    }
IInspectable
  • 46,945
  • 8
  • 85
  • 181

3 Answers3

2

You can use dependency property in the converter.This should help.

Chirag Shah
  • 981
  • 1
  • 6
  • 12
  • Hello! Thanks for your reply! I read the topic you mentioned and edit my code using DependencyProperty. But, here is a little problem. My ListView is Binding to the List in my ViewModel (ItemsSource is set to List), so I have to bind my DependencyProperty to the Item from my list. How can I do that dynamically? – Konstantin Chsherbakov Jun 07 '16 at 09:59
  • 1
    you can bind the dependency property of the converter to the 'sum' textbock using elementname binding. That should work,else I would advise you to have the conversion logic in your viewmodel. – Chirag Shah Jun 07 '16 at 13:13
  • Hello again! As you know, I used the solution presented by Hasan Hasanov with CImbalino toolkit, but here is the problem with that toolkit. It always throws an exception in release mode. So, I returned to your solution using Binding with DependencyProperty, but ElementName binding does not work) Any other ideas? – Konstantin Chsherbakov Jun 13 '16 at 06:34
  • 1
    where have you declared your converter? in the app.xaml or in your page? initialise the converter and do the binding in listview's datatemplate's grid's resources. – Chirag Shah Jun 13 '16 at 06:41
  • Oooooh. Really, that is so easy) I declared it in my page resources. Now I know where is my mistake ) Thank you again :) – Konstantin Chsherbakov Jun 13 '16 at 11:19
  • If you dont mind,can I know the reason why the suggested library dint work in release mode? Any hint about the error,would be of help for others. – Chirag Shah Jun 13 '16 at 12:23
  • sure! You can read here the official reply of cimbalino's author. Click [here](https://github.com/Cimbalino/Cimbalino-Toolkit/issues/52) – Konstantin Chsherbakov Jun 14 '16 at 18:41
0

Binding instead of x:bind solved my problem. maybe this will save someone's a day. Here is the link

Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47
-1

Hello have you tryed binding it as converter parameter?

 <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" />

However you can simulate multibinding in UWP with Cimbalino Windows phone toolkit. Have a look at here

Hasan Hasanov
  • 1,129
  • 2
  • 15
  • 23
  • No, unfortunately it does not work, inside the converter the parameter equals to NULL. I read somewhere that Binding is not supported in ConverterParameter. That is the problem... – Konstantin Chsherbakov Jun 07 '16 at 07:13
  • 1
    You can simulate multibinding behavior with Cimbalino windows phone toolkit: https://www.pedrolamas.com/2013/05/17/cimbalino-windows-phone-toolkit-multibindingbehavior/ – Hasan Hasanov Jun 07 '16 at 09:55
  • I have discovered a problem with that toolkit while publishing application to the Store. This toolkit always throws an exception if you run your application in release mode. This is known issue for the author, but he does not know how to fix that ) – Konstantin Chsherbakov Jun 13 '16 at 06:40
  • Binding not possible with ConverterParameter – Rahul Sonone Sep 27 '16 at 09:03