AliAzra
AliAzra

Reputation: 919

Change StringFormat

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

Upvotes: 0

Views: 358

Answers (2)

mm8
mm8

Reputation: 169200

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>

Upvotes: 0

Nawed Nabi Zada
Nawed Nabi Zada

Reputation: 2875

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.

Upvotes: 1

Related Questions