wpf 中的奇怪风格行为?

发布于 2024-08-07 10:46:32 字数 1285 浏览 5 评论 0原文

好吧,我正在编写一个应用程序,该应用程序在运行时加载合并字典以更改外观和行为,但当我陷入困境时:我的表单上的某些控件没有对我认为它们必须做出反应的样式做出反应。

我试图尽可能地简化问题,并想出了一些如此简单的东西,以至于我担心我忽略了明显的出血,但无论如何这里是:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜雨飘雪 2024-08-14 10:46:32

MergedDictionaries 一直很奇怪,您可以在其中设置您想要的任何资源,但它们在运行时处理外部资源字典引用。

    <ResourceDictionary.MergedDictionaries>

        <ResourceDictionary Source="StylesDictionary.xaml"/>

    </ResourceDictionary.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.

    <ResourceDictionary.MergedDictionaries>

        <ResourceDictionary Source="StylesDictionary.xaml"/>

    </ResourceDictionary.MergedDictionaries>

The only requirement is that the resources be set in an other dictionary.

埋情葬爱 2024-08-14 10:46:32

试试这个...

<Style TargetType="{x:Type TextBox}">

Try this...

<Style TargetType="{x:Type TextBox}">
意中人 2024-08-14 10:46:32

我不确定这是否有效,但是当我遇到样式问题时,我使用以下方法修复了它们

TargetType="{x:Type TextBox}"

I'm not sure if this will work, but when I was having styling problems, I fixed them by using

TargetType="{x:Type TextBox}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文