Josh Smith just posted a new pattern for "Virtual Branches" in WPF - allowing bindings on elements which are not in the logical tree. It works by rerouting the DataContext through StaticResource references. It's undoubtedly interesting, but I'm left wondering where it will actually need to be applied - in many scenarios I think there are simpler strategies than can be used.
In the particular example that Josh has used there is some extra complexity that results from a ValidationRule not being a FrameworkElement (can someone again tell me why ValidationRule isn't an interface?). In essence another boxing FrameworkElement is required to store a bound value.
As it turns out, we can greatly simplify the example, to the point where we don't even need the Virtual Branch.
First we can demote the divisor back into an ordinary property on the ValidationRule:
public class IsMultipleOfValidationRule : ValidationRule { int divisor; public int Divisor { get { return divisor; } set { divisor = value; } } ... }
Now although we can't set a binding on the divisor itself, we can use the same technique used to push the DataContext into a resource. If we place the ValidationRule in a resource dictionary we can push the divisor directly.
<...Resources> <local:IsMultipleOfValidationRule xmlns:local="clr-namespace:" x:Key="rule" /> </...Resources> <Slider Value="{Binding Divisor, Mode=OneWayToSource, Source={StaticResource rule}}" /> <TextBox x:Name="dividendTextBox"> <TextBox.Text> <Binding Path="Target"> <Binding.ValidationRules> <StaticResource ResourceKey="rule" /> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox>
This has the restriction that the value which we are pushing must be an unbound dependency property. Were it not, we could use my ever-useful Proxy.
