I found that the solution could be an auxiliary class with DependencyProperty.
Auxiliary class
public class TestClass : FrameworkElement
{
public string MyProperty
{
get { return (string)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(TestClass), new PropertyMetadata(""));
}
Binding conversion to String
Binding b = new Binding("MyProperty") { Source = myobject };
TestClass tc = new TestClass { DataContext = b };
BindingOperations.SetBinding(tc, TestClass.MyPropertyProperty, b);
string txt = tc.MyProperty;
Advantages:
- You can use Binding and MultiBinding
- You can use Converters
Disadvantages:
- Each time we create a class that inherits from the FrameworkElement, which means that we do unnecessary operations.