元数据覆盖被忽略?

发布于 2024-10-26 03:25:20 字数 2344 浏览 0 评论 0原文

我做了一个非常简单的测试项目:

MainWindow.xaml:

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Test"
        Title="MainWindow" Height="350" Width="525" VerticalAlignment="Center" HorizontalAlignment="Center">

    <StackPanel x:Name="mainPanel" />

</Window>

MainWindow.xaml.cs:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Test
{
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();

            MyTextBox myTextBox = new MyTextBox("some text here");

            mainPanel.Children.Add(myTextBox);
      }
   }
}

MyTextBox.cs:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Test
{
    class MyTextBox : TextBox
    {
        static MyTextBox()
        {
            MyTextBox.BackgroundProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata(Brushes.Red));
        }

        public MyTextBox(string Content)
        {
            Text = Content;
        }
    }
}

this,为了测试metaData Overriding功能。

现在的问题是:这并不像我预期的那样工作......

事实上,MyTextBox 的背景是白色的,而不是红色的。

我调查并尝试将此作为我的自定义类的构造函数:

public MyTextBox(string Content)
{
    Text = Content;
    Background = Brushes.Blue;
    ClearValue(BackgroundProperty);
}

现在这是我在调试时发现的结果:

在主类中:

MyTextBox myTextBox = new MyTextBox("some text here");

我们进入自定义类的静态构造函数,然后在实例的构造函数中:

Text = Content;>>背景=红色

背景=Brushes.Blue;>>背景=蓝色

ClearValue(BackgroundProperty);>>再次Background = Red(如预期)

我们回到主类:

mainPanel.Children.Add(myTextBox);

...在这行代码之后,myTextBox.Background 是白色。

问:为什么?

为什么当我将其添加到 mainPanel 时它设置为白色???我找不到对此的任何逻辑解释...

此外,如果我添加更多代码,我会执行以下操作: myTextBox.Background = Brushes.Blue; 然后 myTextBox.ClearValue(MyTextBox .BackgroundProperty);,它再次变为蓝色,然后变为白色,而不是红色。

我不明白。

I have made a very simple test project:

MainWindow.xaml:

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Test"
        Title="MainWindow" Height="350" Width="525" VerticalAlignment="Center" HorizontalAlignment="Center">

    <StackPanel x:Name="mainPanel" />

</Window>

MainWindow.xaml.cs:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Test
{
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();

            MyTextBox myTextBox = new MyTextBox("some text here");

            mainPanel.Children.Add(myTextBox);
      }
   }
}

MyTextBox.cs:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Test
{
    class MyTextBox : TextBox
    {
        static MyTextBox()
        {
            MyTextBox.BackgroundProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata(Brushes.Red));
        }

        public MyTextBox(string Content)
        {
            Text = Content;
        }
    }
}

this, in order to test the metaData Overriding function.

now the trouble is: this does not work as I expected it to...

indeed, the MyTextBox's background is white, and not Red.

I investigated and tried this as constructor for my custom class:

public MyTextBox(string Content)
{
    Text = Content;
    Background = Brushes.Blue;
    ClearValue(BackgroundProperty);
}

now here is what I found out when I debugged:

in the main class:

MyTextBox myTextBox = new MyTextBox("some text here");

we go into the custom class's static constructor, then in the instance's constructor:

Text = Content; >> Background = Red

Background = Brushes.Blue; >> Background = Blue

ClearValue(BackgroundProperty); >> Background = Red again (as expected)

we go back to the main class:

mainPanel.Children.Add(myTextBox);

... and right after this line of code, myTextBox.Background is White.

Question: Why?

why is this set to white when I add it to the mainPanel??? I cannot find any logical explanation to this...

furthermore, if I add some more code where I do something like: myTextBox.Background = Brushes.Blue; and then myTextBox.ClearValue(MyTextBox.BackgroundProperty);, it becomes Blue then White again, instead of Red.

I don't understand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

温暖的光 2024-11-02 03:25:20

背景由文本框的默认样式设置。根据依赖属性值优先级,红色位于#11,而默认样式位于#9。蓝色设置为#3,因此应该覆盖背景罚款。

您要么必须显式设置背景(就像使用蓝色画笔一样),要么创建自己的自定义默认样式,但不设置背景。您的默认样式可以基于 TextBox 版本。

The Background is being set by the default Style of the TextBox. Based on the Dependency Property Value Precedence Red is at #11, while the default Style is at #9. The Blue setting would be at #3, so that should override the Background fine.

You would either have to set the background explicitly (like you do with the Blue brush), or create your own custom default style that doesn't set the Background. Your default style can be based on the TextBox version.

玻璃人 2024-11-02 03:25:20

您可以以应用于 MyTextBox 的样式设置 Background

<Application.Resources>
    <Style TargetType="local:MyTextBox">
        <Setter Property="Background" Value="Red" />
    </Style>
</Application.Resources>

正如 CodeNaked 提到的,您的默认元数据值将被文本框的默认样式覆盖。如果您更改代码,您可以看到它:

MyTextBox.cs:

    Control.BackgroundProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata(Brushes.Red, 
            FrameworkPropertyMetadataOptions.Inherits, PropertyChangedCallback));

    private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        // set breakpoint here
    }

当断点被打破时,您将能够看到 OldValueRedNewValue< /code> 是 White 并且从堆栈跟踪中您可以看到它是由于应用了默认样式而发生的。

You can set Background in a style applied to your MyTextBox:

<Application.Resources>
    <Style TargetType="local:MyTextBox">
        <Setter Property="Background" Value="Red" />
    </Style>
</Application.Resources>

As CodeNaked mentioned your default metadata value is being overriden by default style for textbox. You can see it if you will change your code:

MyTextBox.cs:

    Control.BackgroundProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata(Brushes.Red, 
            FrameworkPropertyMetadataOptions.Inherits, PropertyChangedCallback));

    private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        // set breakpoint here
    }

When breakpoint will be breaked you will be able to see that OldValue was Red, NewValue is White and from stack-trace you can see that it happens because of default style being applied.

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