【问题标题】:XAML: Refer to StaticResource in plain XAML? (without markup extension)XAML:在纯 XAML 中引用 StaticResource? (没有标记扩展)
【发布时间】:2011-03-11 13:01:48
【问题描述】:

我正在对一系列文本框设置验证规则。我宁愿不为每个 TextBox 创建自定义验证规则的新实例...

<Window.Resources>
  <my:IsIntegerRule x:Key="IsIntegerRule"/>
</Window.Resources>

...
...

<TextBox>
    <TextBox.Text>
      <Binding XPath="@num" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
         <Binding.ValidationRules>

            <-- WHAT IS THE EQUIVALENT OF WRITING: {StaticResource IsIntegerRule} here -->

         </Binding.ValidationRules>
      </Binding>
     </TextBox.Text>
 </TextBox>

谁能帮忙?

【问题讨论】:

    标签: wpf vb.net data-binding xaml validation


    【解决方案1】:

    您可以对标记扩展使用普通的属性元素语法。见Markup Extensions and WPF XAML。它看起来像这样:

    <Binding.ValidationRules>
        <StaticResource ResourceKey="IsIntegerRule"/>
    </Binding.ValidationRules>
    

    【讨论】:

    • 感谢@Quartermeister,一旦我把它放在我的代码中,我在 XAML 编辑器中收到一个错误提示 Invalid type。然而,它就像魅力一样。
    猜你喜欢
    • 2012-02-01
    • 2016-08-13
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 2011-03-10
    相关资源
    最近更新 更多