在XAML中是否可以并排设置UserControl.Resources中的ResourceDictionary和Style?

时间:2011-09-22 03:05:55

标签: wpf xaml styles resourcedictionary

当我已经设置了ResourceDictionary时,是否可以设置内联样式?

这是我已经设定的......

 <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/My.Project.Common.Desktop;component/Themes/StandardStyles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </UserControl.Resources>

以下是我需要添加到UserControl.Resources节点的样式......

 <Style x:Key="MessageErrorIcon" TargetType="{x:Type Rectangle}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Icon}" Value="Asterisk">
                    <Setter Property="Fill" Value="{DynamicResource MessageOverlayInformationIcon}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Icon}" Value="Error">
                    <Setter Property="Fill" Value="{DynamicResource MessageOverlayErrorIcon}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Icon}" Value="Exclamation">
                    <Setter Property="Fill" Value="{DynamicResource MessageOverlayExclamationIcon}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Icon}" Value="Hand">
                    <Setter Property="Fill" Value="{DynamicResource MessageOverlayErrorIcon}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Icon}" Value="Information">
                    <Setter Property="Fill" Value="{DynamicResource MessageOverlayInformationIcon}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Icon}" Value="Question">
                    <Setter Property="Fill" Value="{DynamicResource MessageOverlayQuestionIcon}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Icon}" Value="Stop">
                    <Setter Property="Fill" Value="{DynamicResource MessageOverlayErrorIcon}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Icon}" Value="Warning">
                    <Setter Property="Fill" Value="{DynamicResource MessageOverlayExclamationIcon}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

我需要在UserControl级别设置这些样式,因为它们绑定到数据上下文。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

你可以添加这样的资源

<UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/My.Project.Common.Desktop;component/Themes/StandardStyles.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <Style x:Key="MessageErrorIcon"
                   TargetType="{x:Type Rectangle}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Icon}"
                                 Value="Asterisk">
                        <Setter Property="Fill"
                                Value="{DynamicResource MessageOverlayInformationIcon}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Icon}"
                                 Value="Error">
                        <Setter Property="Fill"
                                Value="{DynamicResource MessageOverlayErrorIcon}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Icon}"
                                 Value="Exclamation">
                        <Setter Property="Fill"
                                Value="{DynamicResource MessageOverlayExclamationIcon}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Icon}"
                                 Value="Hand">
                        <Setter Property="Fill"
                                Value="{DynamicResource MessageOverlayErrorIcon}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Icon}"
                                 Value="Information">
                        <Setter Property="Fill"
                                Value="{DynamicResource MessageOverlayInformationIcon}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Icon}"
                                 Value="Question">
                        <Setter Property="Fill"
                                Value="{DynamicResource MessageOverlayQuestionIcon}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Icon}"
                                 Value="Stop">
                        <Setter Property="Fill"
                                Value="{DynamicResource MessageOverlayErrorIcon}" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Icon}"
                                 Value="Warning">
                        <Setter Property="Fill"
                                Value="{DynamicResource MessageOverlayExclamationIcon}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ResourceDictionary>
    </UserControl.Resources>