元数据覆盖被忽略?
我做了一个非常简单的测试项目:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
背景由文本框的默认样式设置。根据依赖属性值优先级,红色位于#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.
您可以以应用于
MyTextBox
的样式设置Background
:正如 CodeNaked 提到的,您的默认元数据值将被文本框的默认样式覆盖。如果您更改代码,您可以看到它:
MyTextBox.cs:
当断点被打破时,您将能够看到
OldValue
为Red
,NewValue< /code> 是
White
并且从堆栈跟踪中您可以看到它是由于应用了默认样式而发生的。You can set
Background
in a style applied to yourMyTextBox
: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:
When breakpoint will be breaked you will be able to see that
OldValue
wasRed
,NewValue
isWhite
and from stack-trace you can see that it happens because of default style being applied.