I know this is an old question, but maybe it will be useful for someone who came across it. The solution I found is the following:
public class WattHoursConverter : FrameworkElement, IValueConverter { #region Unit (DependencyProperty) /// <summary> /// A description of the property. /// </summary> public string Unit { get { return (string)GetValue(UnitProperty); } set { SetValue(UnitProperty, value); } } public static readonly DependencyProperty UnitProperty = DependencyProperty.Register("Unit", typeof(string), typeof(WattHoursConverter), new PropertyMetadata("", new PropertyChangedCallback(OnUnitChanged))); private static void OnUnitChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((WattHoursConverter)d).OnUnitChanged(e); } protected virtual void OnUnitChanged(DependencyPropertyChangedEventArgs e) { } #endregion public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // you can use the dependency property here ... } }
and in your xaml:
<UserControl.Resources> <converters:WattHoursConverter x:Key="WattHoursConverter" Unit="{Binding UnitPropFromDataContext}"/> </UserControl.Resources> .... <TextBlock Grid.Column="1" TextWrapping="Wrap" Text="{Binding TotalCO2, Converter={StaticResource KgToTonnesConverter}}" FontSize="13.333" />
Adam Bilinski Feb 15 '13 at 10:30 2013-02-15 10:30
source share