结合对非的UIElement [英] Binding on a Non-UIElement

查看:126
本文介绍了结合对非的UIElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有绑定的问题。由于的RelativeSource 需要可视化树旅行并找到想要的祖先,你只能把它用在一个的UIElement 但我试图做一个的RelativeSource 上的非的UIElement结合,比如是一个有效性规则,这大家都知道里面的心不是的VisualTree 还是其的UIElement 。正如你可以期望绑定休息。 的RelativeSource 就像我说的有没有的VisualTree LogicalTree 可用。我需要它,虽然工作。

I am having problems with Binding. Since RelativeSource needs the visual tree to travel up and find the desired Ancestor, you are only allowed to use it on an UIElement but I am trying to do a RelativeSource binding on an Non-UIElement, such as is a ValidationRule, which as you all know isnt inside the VisualTree nor its UIElement. As you can expect the binding breaks. RelativeSource couldn't be found because like i said there is no VisualTree or LogicalTree available. I need to make it work though.

下面是XAML的例子:

Here is an example of XAML:

<StackPanel DataContext{Binding}>
  <Grid>
    <ContentControl Content{Binding MVPart1>
      <TextBox>
       <TextBox.Text>
        <Binding Path="VMPart1Property1">
         <Binding.ValidationRules>
           <my:MyValidationRule>
            <my:ValidationRule.DOC>
             <my:DepObjClass DepProp={Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}/>
            </my:ValidationRule.DOC>
          </Binding.ValidationRules>
         </Binding>
       </TextBox.Text>
      </TextBox>  
    </ContentControl>
  </Grid>
</StackPanel>

所以基本上MyValidationRule从有效性规则类derivering,但那不是的UIElement也不DependencyObject的,所以我不得不创建一个衍生物自DependencyObject叫DepObjClass要能写下来的XAML绑定前pression一类。

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.

下面是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)......);
}

现在总结一下。 MyValidatonRule没有的UIElement它不是DependencyObject的,但它有一个类型,它的属性,因此,为什么XAML绑定前pression编译。

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.

当我运行结合自身工作心不是因为StackPanel中不可能找到,因为有效性规则不必须的VisualTree也不是我的验证规则在逻辑或Visual树参与申请。

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.

问题是我怎么做出这样的情况下工作,如何从非的UIElement发现的StackPanel比如我的有效性规则?

The question is how do I make such case work, how to find StackPanel from an Non-UIElement such as my ValidationRule?

我appologize我的code不comipiling,但我希望你能明白我要怎样做。
我给50分,以你们为正确答案。

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.

推荐答案

您可以执行以下操作:


  1. 创建从可冻结派生,定义了的DependencyProperty要绑定什么助手组件。

  1. Create a helper component which derives from Freezable and defines a DependencyProperty for what you want to bind.

创建这需要辅助部件,类似于你已经做了什么的对象的属性有效性规则。

Create a ValidationRule with a property which takes an object of the helper component, similar to what you have done already.

声明可以绑定到任何你想绑定在一个对象的资源辅助组件的一个实例。可冻结及其派生类继承其宣布的任何资源控制的结合上下文(在逻辑树中的位置),所以你可以创建你绑定。

Declare an instance of the helper component in the Resources of an object which can bind to whatever you want to bind. Freezable and its derived classes inherit the binding context (the location in the logical tree) of any control in whose Resources they are declared, so there you can create your binding.

在声明有效性规则,使用{}的StaticResource的助手组件分配到的有效性规则属性。 StaticResource的工作没有具有约束力的情况下,只要在使用前的资源声明。

When declaring the ValidationRule, use {StaticResource} to assign the helper component to the property in the ValidationRule. StaticResource works without a binding context, as long as the resource is declared before it is used.

该XAML是这样的:

<StackPanel>
  <StackPanel.Resources>
    <my:Helper x:Key="helper" ValProperty="{Binding}"/>
  </StackPanel.Resources>
  <Grid>
      <TextBox DataContext="{Binding MVPart1}">
       <TextBox.Text>
        <Binding Path="VMPart1Property1">
         <Binding.ValidationRules>
           <my:MyValidationRule Helper="{StaticResource helper}"/>
          </Binding.ValidationRules>
         </Binding>
       </TextBox.Text>
      </TextBox>  
  </Grid>
</StackPanel>

这篇关于结合对非的UIElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆