WPF覆盖不同程序集

时间:2016-08-06 15:41:20

标签: c# wpf resourcedictionary

我正在尝试制作一个可以在其他应用程序上引用的插件WPF应用程序,后者可以改变前者的样式。

我的例子是基于Xceed的BusyIndi​​cator。我在我的插件WPF应用程序中有一个BusyIndi​​cator的样式,并希望在其他应用程序上更改该BusyIndi​​cator的样式。

示例:

WPF插件应用程序:我们称之为OverrideBusyIndi​​cator。解决方案如下图所示,其中包含BusyIndi​​cator的MainWindow存在且BusyIndi​​cator样式位于BusyContextResourceDictionary.xaml

enter image description here

BusyContextResourceDictionary.xaml的内容是:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:xceed="http://schemas.xceed.com/wpf/xaml/toolkit"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type xceed:BusyIndicator}">
    <Setter Property="BusyContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Margin="4">
                    <TextBlock Text="Downloading Email" FontWeight="Bold" HorizontalAlignment="Center"/>
                    <StackPanel Margin="4">
                        <TextBlock Text="Downloading message 4/10..."/>
                        <ProgressBar Value="40" Height="15"/>
                    </StackPanel>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Button Grid.Column="0" Content="Pause" HorizontalAlignment="Right" Margin="0 0 2 0"/>
                        <Button Grid.Column="1" Content="Cancel" HorizontalAlignment="Left" Margin="2 0 0 0"/>
                    </Grid>
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

enter image description here

我创建了另一个将引用上面的程序集的解决方案。我们称之为OverrideBusyIndi​​cator2。这个没有主窗口,它的App.xaml只调用另一个OverrideBusyIndi​​cator.MainWindow。然后我添加了一个BusyContextResourceDictionary2.xaml I EXPECT 来覆盖BusyIndi​​cator的样式,但事实并非如此。我能以任何方式实现这种行为吗?

<Application x:Class="OverrideBusyIndicator2.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:xceed="http://schemas.xceed.com/wpf/xaml/toolkit"
         StartupUri="pack://application:,,,/OverrideBusyIndicator;component/MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="BusyContextResourceDictionary2.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

出于示例目的,BusyContextResourceDictionary2.xaml将只更改文本块文本&#34;正在下载电子邮件&#34;到&#34;不下载电子邮件&#34;。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:xceed="http://schemas.xceed.com/wpf/xaml/toolkit"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/OverrideBusyIndicator;component/BusyContextResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>

<!--I am using BasedOn to override the BusyContextResourceDictionary.xaml-->

<Style TargetType="{x:Type xceed:BusyIndicator}" BasedOn="{StaticResource {x:Type xceed:BusyIndicator}}">
            <Setter Property="BusyContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Margin="4">
                            <TextBlock Text="NOT Downloading Email" FontWeight="Bold" HorizontalAlignment="Center"/>**

1 个答案:

答案 0 :(得分:0)

查看xceed:BusyIndi​​cator控件的样式会很有帮助。您可能需要为控件定义default style key property