【问题标题】:Binding on a Non-UIElement绑定到非 UIElement
【发布时间】:2013-03-30 12:55:44
【问题描述】:

我遇到了绑定问题。由于RelativeSource 需要视觉树向上移动并找到所需的祖先,因此您只能在UIElement 上使用它,但我正在尝试对非UIElement 进行RelativeSource 绑定,例如ValidationRule,众所周知,它既不在VisualTree 内,也不在UIElement 内。正如您所料,绑定中断。找不到RelativeSource,因为就像我说的,没有可用的VisualTree 或LogicalTree。不过我需要让它工作。

这是 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 派生自 ValidationRule 类,但那不是 UIElement 也不是 DependencyObject,因此我必须创建一个派生自 DependencyObject 的类,称为 DepObjClass,以便能够写下 xaml 绑定表达式。

代码如下:

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 绑定表达式编译的原因。

当我运行应用程序时,绑定本身不起作用,因为无法找到 StackPanel,因为 ValidationRule 没有 VisualTree,我的验证规则也没有参与逻辑或可视树。

问题是我如何使这种情况起作用,如何从非 UIElement(例如我的 ValidationRule)中找到 StackPanel?

我为我的代码没有编译而道歉,但我希望你能理解我想要做什么。 我给你们50分的正确答案。

【问题讨论】:

  • 也许像this 这样的东西可以提供帮助。
  • 您到底想达到什么目的?您的 MyValidationRule 类具有 DependencyObject 类型的属性,但实际上并没有使用它。
  • 是的,dependeyobject 有一个依赖属性,我在 xaml 中绑定,但绑定 aint 工作,因为在 visualtree 上,因为验证本身不参与 visualtree,所以我如何使它工作
  • 我的意思是你要解决什么问题?为什么需要在验证规则中进行绑定?
  • @dowhilefor 有那些树解决方案,但它们似乎非常“以某种方式调整它以使其工作”。

标签: wpf binding uielement


【解决方案1】:

您可以执行以下操作:

  1. 创建一个从 Freezable 派生的辅助组件,并为您要绑定的内容定义一个 DependencyProperty。

  2. 创建一个带有属性的 ValidationRule,该属性接受帮助组件的对象,类似于您已经完成的操作。

  3. 在对象的资源中声明一个辅助组件的实例,该对象可以绑定到您想要绑定的任何对象。 Freezable 及其派生类继承了在其资源中声明它们的任何控件的绑定上下文(逻辑树中的位置),因此您可以在其中创建绑定。

  4. 在声明 ValidationRule 时,使用 {StaticResource} 将帮助程序组件分配给 ValidationRule 中的属性。只要在使用资源之前声明了资源,StaticResource 就可以在没有绑定上下文的情况下工作。

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>

【讨论】:

  • 正如我所说,这是 Freezable 的特色。当您说这不适用于其他课程时,您是对的。但是例如,您可以在 Style 中使用 Binding,这是因为 Style 是从 Freezable 派生的。 DataContext 需要在声明 Binding 的地方可用,并且 Freezables 继承它,即使它们被声明为资源。当 helper 设置为 ValidationRule 时,Binding 已经到位。我建议你试试...
  • 我删除了我之前的评论。这个更好。解释一下 my:Helper in resources 是如何知道 DataContext 的。资源中没有 DataContext。一旦解决了在 A CONTROL 初始化时的 StaticResources,就会调用 DataContext。 Resources 中的对象不知道 DataContext,但是一旦它们在运行时的某个时间点在控件上调用,它们就会知道。所以问题是 DataContext 是从哪里来的 ValidationRule 不是 FrameworkElement? ValidationRule 甚至不在 VisualTree 中。
  • 样式在控件初始化时被调用,这就是样式如何成为 DataContext。也许 Style 不是很好的例子。
  • @您的第二条评论:您的假设有问题的是 DataContext 在使用 StaticResource 的上下文中解析:当使用 StaticResource 时,DataContext 在资源所在的上下文中解析被声明,然后才将完成的结果分配给采用 StaticResource 的任何属性
  • @您的第三条评论:我认为您在这里将 Style 与 ControlTemplate 混淆了,它当然可以设置为样式的一部分,但会在实例化的地方创建自己的可视化树片段......无论如何,这个讨论毫无结果。这个技术我以前用过,所以除非你尝试了它并不起作用,否则我无话可说......
猜你喜欢
  • 1970-01-01
  • 2015-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
  • 2011-04-19
  • 1970-01-01
相关资源
最近更新 更多