Link to converter parameter

Can I link to ConverterParameter in Silverlight 4.0?

For example, I would like to do something similar and bind ConverterParameter to an object in the ViewModel, for example.

If this is not possible, are there any other options?

<RadioButton Content="{Binding Path=Mode}" IsChecked="{Binding Converter={StaticResource ParameterModeToBoolConverter}, ConverterParameter={Binding Path=DataContext.SelectedMode,ElementName=root}}" /> 
+43
converter binding xaml
Dec 08 2018-10-12
source share
3 answers

Unfortunately, no, you cannot bind to ConverterParameter. There are two options that I have used in the past: instead of using a converter, create a property on your ViewModel (or whatever binding you need) that does the conversion for you. If you still want to follow the path of the converter, transfer the entire related object to the converter, and then you can do your calculation in this way.

+53
Jan 29 '11 at 8:23
source share

Another option is to get fancy by creating a custom converter that transfers your other converter and passes it into the converter parameter from the property. As long as this custom converter inherits DependencyObject and uses DependencyProperty, it can be bound. For example:

 <c:ConverterParamHelper ConverterParam="{Binding ...}"> <c:ConverterParamHelper.Converter> <c:RealConverter/> </c:ConverterParamHelper.Converter> </c:ConverterParamHelper> 
+20
Jan 13 '12 at 23:50
source share

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" /> 
+15
Feb 15 '13 at 10:30
source share



All Articles