tags:

views:

40

answers:

2

I'm getting the following error:

The property 'Resources' is set more than once.

Here is my XAML:

<UserControl.Resources>
    <!--Resource dictionaries for framework stuff-->
    <ResourceDictionary>
        <Style x:Key="MultiLineTextBox" TargetType="TextBox">
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="TextWrapping" Value="WrapWithOverflow"/>
        </Style>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/View;component/Common/ResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

    <!--Convertors needed for proper display-->
    <c:CollapsedIfNegative x:Key="CollapseIfNegative"/>
    <c:VisibleIfNegative x:Key="MakeVisibleIfNegative"/>
    <c:ErrorCodeToString x:Key="ConvertErrorCodeToString"/>
</UserControl.Resources>
A: 

Actually, copying your XAML and pasting it in my own UserControl builds just fine (provided that I add the referenced converter classes).

Are you seeing any other errors in your error list, or is this the only one? Sometimes, if another error occurs (such as failure to find a resource), it could cause another compilation error to occur.

Charlie
Error 45 Property elements cannot be in the middle of an element's content. They must be before or after the content.
Adam S
+1  A: 

The .Resources property in Xaml is clever: it's type ResourceDictionary but, if you don't explicitly put a <ResourceDictionary> tag around its content, the compiler will magically assume one for you. That's why you can usually just put your brushes straight into the markup.

However, you've started off by putting in your own ResourceDictionary - which I suspect has prevented that automatic behaviour - and so the compiler now thinks you're trying to set more than one value. If you rewrite like this you should get the result you're after:

<UserControl.Resources>
    <!--Resource dictionaries for framework stuff-->
    <ResourceDictionary>
        <!--Convertors needed for proper display-->
        <!-- move this INSIDE the ResourceDictionary tag -->
        <c:CollapsedIfNegative x:Key="CollapseIfNegative"/>
        <c:VisibleIfNegative x:Key="MakeVisibleIfNegative"/>
        <c:ErrorCodeToString x:Key="ConvertErrorCodeToString"/>


        <Style x:Key="MultiLineTextBox" TargetType="TextBox">
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="TextWrapping" Value="WrapWithOverflow"/>
        </Style>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/View;component/Common/ResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
Dan Puzey
This works, but I do not understand why it takes issue with the converters being the last element in <ResourceDictionary> but does not take issue with them being the first.
Adam S
In your example they're not inside the `ResourceDictionary` at all. I think the `MergedDictionaries` element has to be first or last, but other than that the order doesn't matter.
Dan Puzey