none
Assigning a ValidationRule defined in Resources RRS feed

  • Question

  • Let us assume I have a UserControl with a ValidationRule called "myRule" which needs to be defined as a resource.  (This is because it uses a virtual branch to bind properties on the rule to the UserControl.)  I have a TextBox with a binding on the TextBox.Text property.  I need to assign the ValidationRule defined in UserControl.Resources.  However, since the Binding.ValidationRules property is read-only, I need to use the collection markup syntax.

    Code Block

    <TextBox.Text>
      <Binding Path="Value">
        <Binding.ValidationRules>
          <!-- refer to {StaticResource myRule} -->
        </Binding.ValidationRules>
      </Binding>
    </TextBox.Text>


    Again, I cannot simply do this, because my rule needs to be defined as a resource.

    Code Block

    <TextBox.Text>
      <Binding Path="Value">
        <Binding.ValidationRules>
          <local:MyValidationRule/>
        </Binding.ValidationRules>
      </Binding>
    </TextBox.Text>


    How can I accomplish this in XAML?
    Monday, January 7, 2008 3:47 PM

Answers

  • You can use the following syntax to refer to ValidationRule specified in Resources:

    Code Block
    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <
    Page.Resources>
    <
    ExceptionValidationRule x:Key="rule"/>
    </
    Page.Resources>
    <
    TextBox>
    <
    TextBox.Text>
    <
    Binding Path="Value">
    <
    Binding.ValidationRules>
    <
    StaticResource ResourceKey="rule"/>
    </
    Binding.ValidationRules>
    </
    Binding>
    </
    TextBox.Text>
    </
    TextBox>
    </
    Page>

    Hope this helps
    Wednesday, January 9, 2008 7:32 AM