我做了一个非常简单的测试项目:忽略元数据覆盖?
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;
}
}
}
这是为了测试metaData Overriding函数。
现在麻烦的是:因为我预料到这个不工作...
事实上,MyTextBox的背景是白色的,而不是红色。
我调查,并试图以此作为构造为我的自定义类:
public MyTextBox(string Content)
{
Text = Content;
Background = Brushes.Blue;
ClearValue(BackgroundProperty);
}
现在这里是我发现了什么,当我调试:
在主类:
MyTextBox myTextBox = new MyTextBox("some text here");
我们进入自定义类的静态构造函数,然后在实例的构造函数中:
Text = Content;
>>背景=红
Background = Brushes.Blue;
>>背景=蓝色
ClearValue(BackgroundProperty);
>>背景=红了起来(如预期)
我们回到主类:
mainPanel.Children.Add(myTextBox);
...并且在这行代码之后,myTextBox.Background
是白色。
问:为什么?
为什么当我将它添加到mainPanel时,它被设置为白色?此外,如果我再添加一些代码,例如:myTextBox.Background = Brushes.Blue;
,然后myTextBox.ClearValue(MyTextBox.BackgroundProperty);
,它会变成蓝色,然后是白色,而不是红色。
我不明白。
我很怀疑,但一直没能找到这种风格优先文件。虽然这有点奇怪...它损害了元数据的可用性。在我看来是覆盖。 (在视觉特性方面无效) – David 2011-03-25 13:05:50