I have a custom ValidationRule that requires access to the ViewModel in order to validate a supplied value in conjunction with other properties of the ViewModel. I previously tr
I use a different approch. Use Freezable objects to make your bindings
public class BindingProxy : Freezable
{
static BindingProxy()
{
var sourceMetadata = new FrameworkPropertyMetadata(
delegate(DependencyObject p, DependencyPropertyChangedEventArgs args)
{
if (null != BindingOperations.GetBinding(p, TargetProperty))
{
(p as BindingProxy).Target = args.NewValue;
}
});
sourceMetadata.BindsTwoWayByDefault = false;
sourceMetadata.DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
SourceProperty = DependencyProperty.Register(
"Source",
typeof(object),
typeof(BindingProxy),
sourceMetadata);
var targetMetadata = new FrameworkPropertyMetadata(
delegate(DependencyObject p, DependencyPropertyChangedEventArgs args)
{
ValueSource source = DependencyPropertyHelper.GetValueSource(p, args.Property);
if (source.BaseValueSource != BaseValueSource.Local)
{
var proxy = p as BindingProxy;
object expected = proxy.Source;
if (!object.ReferenceEquals(args.NewValue, expected))
{
Dispatcher.CurrentDispatcher.BeginInvoke(
DispatcherPriority.DataBind,
new Action(() =>
{
proxy.Target = proxy.Source;
}));
}
}
});
targetMetadata.BindsTwoWayByDefault = true;
targetMetadata.DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
TargetProperty = DependencyProperty.Register(
"Target",
typeof(object),
typeof(BindingProxy),
targetMetadata);
}
public static readonly DependencyProperty SourceProperty;
public static readonly DependencyProperty TargetProperty;
public object Source
{
get
{
return this.GetValue(SourceProperty);
}
set
{
this.SetValue(SourceProperty, value);
}
}
public object Target
{
get
{
return this.GetValue(TargetProperty);
}
set
{
this.SetValue(TargetProperty, value);
}
}
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
}
sHould This have the problem of binding the value too late after the application started. I use Blend Interactions to resolve the problem after the window loads