简体   繁体   English

在其他类中实例化UserControl时,不使用资源中的样式

[英]Styles from resources aren't used when UserControl is instantiated in other class

I create custom WPF Expander inherited from common Expander. 我创建从通用扩展器继承的自定义WPF扩展器。 I have a file with styles at path MyAssembly/Theme/generic.xaml . 我在路径MyAssembly/Theme/generic.xaml有一个具有样式的MyAssembly/Theme/generic.xaml I can see that styles are applied in designer but are not in running program . 我可以看到样式在设计器中应用,但不在运行的程序中

MyExpander.xaml: MyExpander.xaml:

<Expander x:Class="Path.To.MyExpander"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
    <Expander.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MyAssembly;component/Theme/generic.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Expander.Resources>
    ...

generic.xaml: generic.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
    ...

code where I instanciate the expanders: 我实例化扩展器的代码:

class Viewer
{
    private void GenerateExpanders()
    {
        this.Expanders.Children.Clear();
        foreach (...)
        {
            MyExpander ex = new MyExpander();
            ex.HeaderText.Text = "Sample";
            ex.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            ex.IsExpanded = true;
            this.Expanders.Children.Add(ex);
        }
    }
}

All three files are in one assembly but class Viewer is instantiated in another assembly. 这三个文件都在一个程序集中,但类Viewer在另一个程序集中实例化。 Is there something am I doing wrong? 我做错什么了吗?

I had 我有

<Style TargetType="Expander"

but it should be 但这应该是

<Style TargetType="MyExpander"

I didn't realise that it wasn't Expander anymore - it was MyExpander ( class MyExpander : Expander ) 我没有意识到它不再是Expander了-它是MyExpander( class MyExpander : Expander

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM