There's quite a lot out there regarding binding a XAML StaticResource, such as a string constant to a XAML control. However I cannot find a good way to do two way binding in this scenario.
I want to bind a global boolean to a checkbox which enables 'debugging' mode, that toggles visibility of certain things throughout my application.
I thought I could solve the problem with the following resource defined in Resources.xaml
. This basically wraps any object with a dependency object that can be bound to:
<local:BindingWrapper x:Key="DebugMode">
<local:BindingWrapper.Value>
<sys:Boolean>false</sys:Boolean>
</local:BindingWrapper.Value>
</local:BindingWrapper>
where BindingWrapper is an instance of this class :
public class BindingWrapper : DependencyObject
{
public object Value
{
get { return (object)GetValue(ValProperty); }
set { SetValue(ValProperty, value); }
}
public static readonly DependencyProperty ValProperty =
DependencyProperty.Register("Value", typeof(object), typeof(BindingWrapper), null);
}
Then throughout my application I can just use the following :
Visibility="{Binding Source={StaticResource DebugMode},Path=Value,
Converter={StaticResource booleanToVisibilityConverter}}"
And I DID solve the problem with this. It works great EXCEPT in the designer, where I end up with the dreaded Element-is-already-the-child-of-another-element error. In both VS2010 and Blend 4 I get this error and cannot edit the control.
SO... whats a good way to achieve TWO WAY binding of a primitive constant in an application resource.