2010-07-04 34 views
3

我在一系列文本框中設置了驗證規則。我寧願不爲每個文本框創建自定義驗證規則的新實例...XAML:請參閱普通XAML中的StaticResource? (無標記擴展名)

<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> 

任何人都可以幫忙嗎?

回答

6

您可以對標記擴展使用標準屬性元素語法。請參閱Markup Extensions and WPF XAML。它看起來像這樣:

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

感謝@Quartermeister,一旦我把它放在我的代碼中我有一個錯誤,說**在XAML編輯器中的無效類型**。然而它像魅力一樣工作。 – Elangesh 2012-01-04 11:22:03