If I understand your question correctly, you have a FrameworkElement that provides a plain old plain regular property that is not reserved as a Dependency property. However, you would like to set it as the target of the binding.
First of all, getting TwoWay to work would be unlikely and impossible in most cases. However, if you only need one way to bind, you can create an attached property as a surrogate for the actual property.
Suppose I have an element in the StatusDisplay structure that has a string Message property that, for some really dumb reason, does not support Message as a dependency property.
public static StatusDisplaySurrogates { public static string GetMessage(StatusDisplay element) { if (element == null) { throw new ArgumentNullException("element"); } return element.GetValue(MessageProperty) as string; } public static void SetMessage(StatusDisplay element, string value) { if (element == null) { throw new ArgumentNullException("element"); } element.SetValue(MessageProperty, value); } public static readonly DependencyProperty MessageProperty = DependencyProperty.RegisterAttached( "Message", typeof(string), typeof(StatusDisplay), new PropertyMetadata(null, OnMessagePropertyChanged)); private static void OnMessagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { StatusDisplay source = d as StatusDisplay; source.Message = e.NewValue as String; } }
Of course, if the StatusDisplay control has the Message property changed for any reason, the state of this surrogate will no longer match. However, this may not be relevant to your goals.
AnthonyWJones
source share