wpf 中的奇怪风格行为?
好吧,我正在编写一个应用程序,该应用程序在运行时加载合并字典以更改外观和行为,但当我陷入困境时:我的表单上的某些控件没有对我认为它们必须做出反应的样式做出反应。
我试图尽可能地简化问题,并想出了一些如此简单的东西,以至于我担心我忽略了明显的出血,但无论如何这里是:
<Window x:Class="Example.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="100" Width="50">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Green"></Setter>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<TextBox Text="1" Name="box1"/>
<TextBox Text="2" Name="box2"/>
<TextBox Text="3" Name="box3"/>
</StackPanel>
</Window>
问题是:为什么第一个文本框不是绿色? ==>也就是说,设计器以绿色显示它,但是当运行应用程序时,它不再......
我知道这个特定问题的解决方案是删除合并的字典标签,但我需要知道如何使用合并来解决这个问题字典。
谢谢!
Ok, I was programming an app that loaded merged dictionaries on runtime to change appearance and behaviour when I got stuck : some of the controls on my forms just weren't reacting to the styles I thought they had to react to.
I have tried to simplify the problem as much as I could and came up with something so simple that I'm afraid I am overlooking the bleeding obvious, but anyway here it goes :
<Window x:Class="Example.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="100" Width="50">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Green"></Setter>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<TextBox Text="1" Name="box1"/>
<TextBox Text="2" Name="box2"/>
<TextBox Text="3" Name="box3"/>
</StackPanel>
</Window>
The question is : why is the first textbox not green?
==> that is, the designer shows it in green, but when running the app, it is no longer...
I know the solotion to this particular problem is to remove the merged dicitonary tags, but I need to know how to solve this using merged dictionaries.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MergedDictionaries 一直很奇怪,您可以在其中设置您想要的任何资源,但它们仅在运行时处理外部资源字典引用。
唯一的要求是在其他字典中设置资源。
MergedDictionaries have always been quirky, you can set any resources you want in them, but they only process outside resource dictionary references at runtime.
The only requirement is that the resources be set in an other dictionary.
试试这个...
Try this...
我不确定这是否有效,但是当我遇到样式问题时,我使用以下方法修复了它们
I'm not sure if this will work, but when I was having styling problems, I fixed them by using