<...Text={Binding SourceProperty, Converter={StaticResource SomethingToSomethingElseConverter} ConverterParameter={Binding AnotherSourceProperty}} />
<-.Resources> <converters:-_ x:Key="-_" BindingParameter1="{Binding -_}" /> </-.Resources>
using System; using System.Globalization; using System.Windows; using System.Windows.Data; namespace BindableConverter { [ValueConversion(typeof(object), typeof(object))] public class BindableConverterBase : DependencyObject, IValueConverter, IMultiValueConverter { #region BindableParameters #region BindableParameter1 public object BindableParameter1 { get { return GetValue(BindableParameter1Property); } set { SetValue(BindableParameter1Property, value); } } public static readonly DependencyProperty BindableParameter1Property = DependencyProperty.Register( nameof(BindableParameter1), typeof(object), typeof(BindableConverterBase), new PropertyMetadata(String.Empty) ); #endregion #region BindableParameter2 public object BindableParameter2 { get { return GetValue(BindableParameter2Property); } set { SetValue(BindableParameter2Property, value); } } public static readonly DependencyProperty BindableParameter2Property = DependencyProperty.Register( nameof(BindableParameter2), typeof(object), typeof(BindableConverterBase), new PropertyMetadata(String.Empty) ); #endregion #region BindableParameter3 public object BindableParameter3 { get { return GetValue(BindableParameter3Property); } set { SetValue(BindableParameter3Property, value); } } public static readonly DependencyProperty BindableParameter3Property = DependencyProperty.Register( nameof(BindableParameter3), typeof(object), typeof(BindableConverterBase), new PropertyMetadata(String.Empty) ); #endregion #endregion #region IValueConverter public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } #endregion #region IMultiValueConverter public virtual object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } public virtual object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } #endregion } }
<Window.Resources> <local:NameAndAgeToVladimirPutinConverter x:Key="NameAndAgeToVladimirPutin" BindableParameter1="{Binding FirstName}" BindableParameter2="{Binding Age}" /> </Window.Resources>
<bc:BindingProxy x:Key="BindingProxy" Data="{Binding}" /> <local:NameAndAgeToVladimirPutinConverter x:Key="NameAndAgeToVladimirPutin" BindableParameter1="{Binding Source={StaticResource BindingProxy}, Path=Data.FirstName}" BindableParameter2="{Binding Source={StaticResource BindingProxy}, Path=Data.Age}"/> ... <TextBlock Grid.Row="0" Text="{Binding Name, Converter={StaticResource NameAndAgeToVladimirPutin}}" />
using System.Windows; namespace BindableConverter { public class BindingProxy : Freezable { protected override Freezable CreateInstanceCore() { return new BindingProxy(); } /// <summary> /// Binding data. /// </summary> public object Data { get { return GetValue(DataProperty); } set { SetValue(DataProperty, value); } } public static readonly DependencyProperty DataProperty = DependencyProperty.Register( nameof(Data), typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null) ); } }
Source: https://habr.com/ru/post/275223/
All Articles