0

I have got two different currency £ and €

I can't use StringFormat={}{0:C0} this because this only shows £ every time.

<TextBox  Text="{Binding MinAmount, StringFormat={}{0:C0}.00}" />

I have got Country property "UK or "EURO" But how am i going to use if statement to change the currency symbol before display it please?

 <GridViewColumn.CellTemplate>
<DataTemplate>
    <StackPanel >
        <TextBox  Text="{Binding MinAmount}" Width="105" VerticalContentAlignment="Center" HorizontalContentAlignment="Right">
            <TextBox.Style>
                <Style TargetType="TextBox">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding CurrencyForSelectedCompany}" Value="UK">
                            <Setter Property="Language" Value="en-GB" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding CurrencyForSelectedCompany}" Value="EIRE">
                            <Setter Property="Language" Value="fr-FR" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>
    </StackPanel>
</DataTemplate>

I put this textbox to make sure trigger checks the correct data

 <TextBox Text="{Binding CurrencyForSelectedCompany}" Width="100" />

But somehow UK or FR doesn't activate trigger

AliAzra
  • 889
  • 1
  • 9
  • 28
  • 3
    Currency is culture specific, either user another culture, any European country with euro or use a converter to convert to the correct format and display it – Nawed Nabi Zada Feb 11 '19 at 12:17
  • For more infor: https://www.wpf-tutorial.com/data-binding/the-stringformat-property/ – Nawed Nabi Zada Feb 11 '19 at 12:18
  • Duplicate? https://stackoverflow.com/q/520115/276994 – Vlad Feb 11 '19 at 12:40
  • Hi Vlad, It is not a Duplicate question. we are talking about a different problem. I need to change the currency depending on the country. Not a single country. – AliAzra Feb 11 '19 at 13:07
  • Thank you Nawed but it is not the answer. I don't want to show two different currency at the same time. I want to show only the correct one. Basically how can i use if statement in xaml code? – AliAzra Feb 11 '19 at 13:08

2 Answers2

1

In case you have your amount and currency in seperate properties you can use a IMultiConverter to show and modify both of the properties from one TextBox.

XAML:

<Window.Resources>
    <localResource:MyMultiCurrencyConverter x:Key="MyMultiCurrencyConverter"/>
</Window.Resources>

<TextBox>
   <TextBox.Text>
       <MultiBinding Converter="{StaticResource MyMultiCurrencyConverter}">
           <Binding Path="MyAmount"/>
           <Binding Path="MyCurrency"/>
       </MultiBinding>
   </TextBox.Text>
</TextBox>

C#:

public class MyMultiCurrencyConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values.Aggregate((a, b) => $"{a} {b}");
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        var toReturn = value.ToString().Split(' ');
        return toReturn;
    }
}

The above is based on two different string properties in the ViewModel. You have to split the value in ConvertBack method and cast to the correct types you are binding to.

Nawed Nabi Zada
  • 2,819
  • 5
  • 29
  • 40
0

You could use a Style with a DataTrigger that binds to your Country property and sets the Language property of the TextBox:

<TextBox Text="{Binding MinAmount, StringFormat={}{0:C0}.00}">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Setter Property="Language" Value="fr-FR" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Country}" Value="UK">
                    <Setter Property="Language" Value="en-GB" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • It makes sense but it is not working with my code. I am not sure. is my trigger is wrong or WPF doesn't like to update it. I edited my question to see – AliAzra Feb 11 '19 at 15:27
  • Make sure that the binding works then. It should if `MinAmount` and `Country` belongs to the same class and are public properties. – mm8 Feb 11 '19 at 15:28
  • They are public properties. from different class – AliAzra Feb 11 '19 at 15:31
  • "Different"? Please clarify yourself. Where are your properties defined? – mm8 Feb 11 '19 at 15:31
  • 1
    You should ask a new question if you have another issue though. How to bind to a property was not part of your original question. – mm8 Feb 11 '19 at 15:32
  • Binding is workind. I used MVVM. the Text box shows that CurrencyForSelectedCompany is "UK" but trigger doesn't accepts it.. – AliAzra Feb 11 '19 at 15:35
  • What TextBox shows this? Does each data object have a CurrencyForSelectedCompany property...? – mm8 Feb 11 '19 at 15:39