UserControl.Resources 中定义的资源无法查看代码中 MergedDictionaries 集中定义的元素
DefaultStyle
s 包含所有 TextBox
es 的 DefaultStyle
:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
Resources.MergedDictionaries.Add(new DefaultStyles());
InitializeComponent();
}
}
然后是 Xaml,我继承该样式并添加更多内容:
<UserControl.Resources>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Foreground" Value="Green "/>
</Style>
</UserControl.Resources>
这会抛出一个 StackOverFlowException
因为我的 DefaultStyle
未找到,因此它尝试自引用。
为什么样式看不到合并字典中的默认样式?
DefaultStyle
s contains a DefaultStyle
for all TextBox
es:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
Resources.MergedDictionaries.Add(new DefaultStyles());
InitializeComponent();
}
}
Then the Xaml, I inherit the style and add a little more:
<UserControl.Resources>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Foreground" Value="Green "/>
</Style>
</UserControl.Resources>
This throws a StackOverFlowException
as my DefaultStyle
is not found, and so it trys to self reference.
Why can't the Style see the default styles in the merged dictionary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它看不到它,因为它本质上正在取代它。
InitializeComponent
方法调用将用您在 XAML 中定义的字典替换您在构造函数中引用的字典。基本上,你做错了。为什么你不能这样做:
It can't see it because it's essentially replacing it. The
InitializeComponent
method call will replace the dictionary you refer to in your ctor with the dictionary you define in your XAML.Basically, you're doing it wrong. Why can't you just do this: