XamlParseException error template

514 views Asked by At

i have an XamlParseException on validation errortemplate and style.

i don't know what the problem is.

xaml:

<DataGridTemplateColumn Header="Naam" 
    CellTemplate="{StaticResource ContactNameTemplate}" 
    CellEditingTemplate="{StaticResource EditingContactNameTemplate}"/>

dictionary:

<DataTemplate x:Key="ContactNameTemplate">
    <TextBox HorizontalAlignment="Center" 
        Validation.ErrorTemplate="{StaticResource validationTemplate}" 
        Style="{StaticResource textBoxInError}">
            <TextBox.Text>
            <Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <c:Text Min="21" Max="130"/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
</DataTemplate>
<DataTemplate x:Key="EditingContactNameTemplate">
    <TextBox HorizontalAlignment="Center" 
      Validation.ErrorTemplate="{StaticResource validationTemplate}" 
      Style="{StaticResource textBoxInError}">
        <TextBox.Text>
            <Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <c:Text Min="21" Max="130"/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
</DataTemplate>
<ControlTemplate x:Key="validationTemplate">
    <DockPanel>
        <TextBlock Foreground="Red" FontSize="20">!</TextBlock>
        <AdornedElementPlaceholder/>
    </DockPanel>
</ControlTemplate>
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip" 
              Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>

model:

public class Text : ValidationRule
{
    private int _min;
    private int _max;

    public Text()
    {
    }

    public int Min
    {
        get { return _min; }
        set { _min = value; }
    }

    public int Max
    {
        get { return _max; }
        set { _max = value; }
    }

    public override ValidationResult Validate(object value, 
      System.Globalization.CultureInfo cultureInfo)
    {
        ValidationResult result;
        string text;
        if (((string)value).Length < Min || ((string)value).Length > Max)
        {
            text = value.ToString();
            result = new ValidationResult(true, null);
        }
        else
        {
            text = "";
            result = new ValidationResult(false, 
              "De naam moet tss " + Min + " en " + Max + " liggen.");
        }
        return result;
    }
}

the error i get

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll .

Additional information: The specified value System.Windows.Markup.StaticResourceHolder threw an exception.

thanks

1

There are 1 answers

0
Lukazoid On BEST ANSWER

XAML resources are loaded in the order they are read; when using a StaticResource, you need to be sure that this resource is available before being used.

To solve this, ensure the definition of the resources validationTemplate and textBoxInError is above ContactNameTemplate and EditingContactNameTemplate.