有没有办法忽略contentpresenter中的应用程序资源

时间:2012-05-16 15:02:41

标签: c# wpf inheritance styling

与问题相关的一些信息:http://www.11011.net/archives/000692.html

具体情况是:在app.xaml中声明了一些通用文本块(键等于键入)样式,这些样式位于第三方应用程序中,我的视图中的所有内容提供者都使用它们忽略了我自己的样式。

我找到了一些可能的解决方案:

  1. 使用覆盖模板&显式分配样式的所有元素将我的样式的资源字典添加到contentpresenter资源。

  2. 为字符串添加datatemplate,但访问文本检测存在问题(可以通过将contentpresenter放入ref我自己的资源来解决,这不是一个好的解决方案,因为我们增加了可视化树只是为了解决这个问题)

  3. 可能还有其他解决方案吗?

    P.S。:已经存在很多观点,所以第一个选项很多工作!

    要重现创建新的wpf项目并修改下一个文件:

    App.xaml添加通用样式:

    <Application.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="FontSize" Value="20"/>
        </Style>
    </Application.Resources>
    

    MainWindow.xaml内容为:

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>     
    </Window.Resources>
    <StackPanel>
        <Button Content="Hello world">
            <Button.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Access_Text"/>
                    <MenuItem Header="NormalText"/>
                </ContextMenu>
            </Button.ContextMenu>
        </Button>        
        <TextBlock Text="WELCOME TO BLACK MESA"/>
    </StackPanel>
    

    添加Dictionary.xaml资源字典并在其中添加下一个样式:

    <Style TargetType="TextBlock">
        <Setter Property="FontSize" Value="8"/>
    </Style>
    

1 个答案:

答案 0 :(得分:0)

不知道你可以在哪里做,因为我不了解你的应用程序的结构,但是如果你想从应用程序资源中删除样式,你可以像这样以编程方式进行

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ResourceDictionary dic = App.Current.Resources;
        dic[typeof (TextBlock)] = null;
    }
}

使用您在测试WPF项目中提供的XAML,这使我的默认字体大小为 12 ,而不是 20