XAML:属性“资源”设置不止一次

时间:2010-08-06 16:22:20

标签: wpf xaml

我收到以下错误:

  

属性'资源'设置得更多   不止一次。

这是我的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>

2 个答案:

答案 0 :(得分:69)

Xaml中的.Resources属性很聪明:它的类型为ResourceDictionary但是,如果您没有在其内容中明确放置<ResourceDictionary>标记,编译器会神奇地为您假设一个。这就是为什么你通常只需将你的画笔直接放入标记。

但是,您已经开始使用自己的ResourceDictionary - 我怀疑它已经阻止了这种自动行为 - 因此编译器现在认为您正在尝试设置多个值。如果你这样重写,你应该得到你想要的结果:

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

答案 1 :(得分:0)

实际上,复制你的XAML并在我自己的UserControl中粘贴它就可以了(假设我添加了引用的转换器类)。

您是否在错误列表中看到任何其他错误,或者这是唯一的错误?有时,如果发生另一个错误(例如找不到资源),则可能导致另一个编译错误。

相关问题