创建自定义WPF扩展器,继承自通用扩展器。我有一个样式在路径MyAssembly/Theme/generic.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:
<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">
...
实例化展开器的代码:
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);
}
}
}
所有三个文件都在一个程序集中,但是类Viewer在另一个程序集中实例化。我做错了什么吗?
我有
<Style TargetType="Expander"
但应该是
<Style TargetType="MyExpander"
我没有意识到它不再是Expander -它是MyExpander (class MyExpander : Expander
)