Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding on a Non-UIElement
    text
    copied!<p>I am having problems with Binding. Since <code>RelativeSource</code> needs the visual tree to travel up and find the desired Ancestor, you are only allowed to use it on an <code>UIElement</code> but I am trying to do a <code>RelativeSource</code> binding on an Non-UIElement, such as is a ValidationRule, which as you all know isnt inside the <code>VisualTree</code> nor its <code>UIElement</code>. As you can expect the binding breaks. <code>RelativeSource</code> couldn't be found because like i said there is no <code>VisualTree</code> or <code>LogicalTree</code> available. I need to make it work though.</p> <p>Here is an example of XAML:</p> <pre><code>&lt;StackPanel DataContext{Binding}&gt; &lt;Grid&gt; &lt;ContentControl Content{Binding MVPart1&gt; &lt;TextBox&gt; &lt;TextBox.Text&gt; &lt;Binding Path="VMPart1Property1"&gt; &lt;Binding.ValidationRules&gt; &lt;my:MyValidationRule&gt; &lt;my:ValidationRule.DOC&gt; &lt;my:DepObjClass DepProp={Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}/&gt; &lt;/my:ValidationRule.DOC&gt; &lt;/Binding.ValidationRules&gt; &lt;/Binding&gt; &lt;/TextBox.Text&gt; &lt;/TextBox&gt; &lt;/ContentControl&gt; &lt;/Grid&gt; &lt;/StackPanel&gt; </code></pre> <p>So basically MyValidationRule is derivering from ValidationRule class, but thats not UIElement nor DependencyObject and therefore I had to create a class which derivates from DependencyObject called DepObjClass to be able to write down the xaml binding expression.</p> <p>Here is code:</p> <pre><code>public class MyValidationRule : ValidationRule { public DepObjClass DOC { get; set; } public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { string text = value as string; if (!string.IsNullOrEmpty(text)) { return new ValidationResult(true, string.Empty); } return new ValidationResult(false, "Not working blahhh"); } } public class DepObjClass : DependencyObject { public object DepProp { get { return (object)GetValue(DepPropProperty); } set { SetValue(DepPropProperty, value); } } public static DependencyProperty DepPropProperty = DependencyProperty.Register(typeof(object), typeof(DepObjClass)......); } </code></pre> <p>Now to sum up. MyValidatonRule is not UIElement its not DependencyObject but it has a property of a type that is, hence why the xaml binding expression compiles.</p> <p>When I run the application the binding itself isnt working because StackPanel couldnt be found because ValidationRule doesnt have VisualTree nor my validation rule participates in Logical or Visual Tree. </p> <p>The question is how do I make such case work, how to find StackPanel from an Non-UIElement such as my ValidationRule?</p> <p>I appologize for my code not comipiling but I hope you can understand what I am trying to do. I am giving 50 points to you guys for the right answer. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload