-1

I have WPF application with DataGrid that contain some prices. I want to dynamically change currency and column format must also be adjusted.

The only way i found is to set format by this way:

<DataGridTextColumn Header="Price" Width="95" Binding="{Binding Path=Price, StringFormat=C, ConverterCulture='en-US'}" />

But this binding is static. When I try to bind ConverterCulture to value from code, it throws binding error.

<DataGridTextColumn Header="Price" Width="95" Binding="{Binding Path=Price, StringFormat=C, ConverterCulture="{Binding Source=CurrencyCulture}" />

Is there any way to dynamically change ConverterCulture in Datagrid column format?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pavel Polushin
  • 351
  • 2
  • 5
  • 16
  • 1
    Bindings can only be specified by the DependecyProperty declared in the DependecyObject. But the Binding itself is a MarkupExtension. And the properties in it (including ConverterCulture) are ordinary CLR properties. If you need to implement this in a binding in one or two properties, then the easiest way is to use a MultiBinding and а multiconverter. – EldHasp Aug 18 '21 at 22:03

3 Answers3

1

Is there any way to dynamically change ConverterCulture in Datagrid column format?

Set Language property of the DataGrid:

<DataGrid Language="fr-FR" ...>

This is a dependency property that you can bind to.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mm8
  • 163,881
  • 10
  • 57
  • 88
0

I did not test it so i cant tell if it works for sure but you can try something like this with a style and a datatrigger. In this example you would need a dependecy property "currency" that you set to "USD" to trigger the change, however you could adjust the trigger to your need:

<DataGridTextColumn Header="Price" Width="95" Binding="{Binding Path=Price, StringFormat=C, ConverterCulture="{Binding Source=CurrencyCulture}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Binding" Value="{Binding Path=Price, StringFormat=C, ConverterCulture="{Binding Source=CurrencyCulture}"
            <Style.Triggers>
                <DataTrigger Binding="Currency" Value="USD">
                    <Setter Property="Binding" Value="{Binding Path=Price, StringFormat=C, ConverterCulture='en-US'}"
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.CellStyle>
<DataGridTextColumn/>

Hope this solves your issue or guides you in a direction that does the trick

Denis Schaf
  • 2,478
  • 1
  • 8
  • 17
0

Finally I found a solution. Not ideal but seems to work. CurrentContext is my static class to store variables. SelectedCurrency.CurrencyCode is 3-char currency code like USD or EUR. When I change currency in Combobox, string format in column also changes with needed currency symbol.

CultureConverter.cs

public class CultureConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values[0] == null) return string.Empty;


            return String.Format(CurrentContext.CultureInfo, "{0:C}", values[0]);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

Form.xaml

<Window.Resources>
        <providers:CultureConverter x:Key="CultureConverter" />
    </Window.Resources>

DataGrid Name="Datagrid" ItemsSource="{Binding Items, Mode=TwoWay}" AutoGenerateColumns="False">
  <DataGrid.Columns>
...
    <DataGridTemplateColumn>
      <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
           <TextBlock>
             <TextBlock.Text>
               <MultiBinding Converter="{StaticResource CultureConverter}">
                 <Binding Path="InitialCost" />
               </MultiBinding>
             </TextBlock.Text>
           </TextBlock>
         </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
   </DataGridTemplateColumn>
...
  </DataGrid.Columns>
</DataGrid>
private void CB_Currency_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

    CurrentContext.CultureInfo = 
    CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                          .Where(x => new RegionInfo(x.LCID)
                          .ISOCurrencySymbol == SelectedCurrency.CurrencyCode)
                          .First();
}
Pavel Polushin
  • 351
  • 2
  • 5
  • 16