Reputation: 95
I would like to bind currency for text column and if i type the value like "de" it is working and i can see Euro. But if I try to bind it it's sending an error.
Ofc i can bind the currency to whole View, but i have always 2 different currencies to display.
There is code that is not working:
Binding="{Binding Income,StringFormat=C,ConverterCulture={Binding CultureFormat}}" />
and If i do like this it's working properly:
<DataGridTextColumn Header="Saldo" Binding="{Binding Balance,StringFormat=C,ConverterCulture=de}" />
I found that the is some solution to use multibinding, but i don't know how in this example.
Upvotes: 0
Views: 708
Reputation: 169190
Here is how you could to this using a multi binding and a converter:
public class CultureConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if(values[0] != DependencyProperty.UnsetValue && values[1] != DependencyProperty.UnsetValue)
{
decimal balance = System.Convert.ToDecimal(values[0]);
string c = values[1] as string;
return balance.ToString("C", CultureInfo.GetCultureInfo(c));
}
return Binding.DoNothing;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
<DataGrid ...>
<DataGrid.Resources>
<local:CultureConverter x:Key="conv" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Saldo">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource conv}">
<Binding Path="Income" />
<Binding Path="CultureFormat" />
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
...
</DataGrid.Columns>
</DataGrid>
The example assumes that the CultureFormat
property returns a string. If it returns a CultureInfo
you could just cast values[1]
to this type instead of string
.
Upvotes: 1
Reputation: 5666
Binding
is not a dependecy object and its properties are not dependency one. To apply a binding to Binding.ConverterCulture
the source should be a dependency property and I guess that CultureFormat
is not.
If you need a variable culture info, you can format your data in the ViewModel using a specific property, something like
public string BalanceString
{
get { return String.Format(CultureFormat, "C", Balance); }
}
and then use this new property for binding:
<DataGridTextColumn Header="Saldo" Binding="{Binding BalanceString, Mode=OneWay}" />
Upvotes: 0