Ignoring metadata?

I 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; } } } 

to check the metadata override function.

Now the problem is: it does not work as I expected ...

indeed, the background of MyTextBox is white, not red.

I researched and tried this as a constructor for my custom class:

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

Now here is what I learned when debugging:

in the main class:

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

we will move on to the static constructor of the custom class, then in the instance constructor:

Text = Content; → Background = red

Background = Brushes.Blue; → Background = blue

ClearValue(BackgroundProperty); → Background = red again (as expected)

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 parameter set to white when i add it to mainPanel ??? I can not find a logical explanation for this ...

Also, if I add some more code where I do something like: myTextBox.Background = Brushes.Blue; and then myTextBox.ClearValue(MyTextBox.BackgroundProperty); , it turns blue, and then again white, not red.

I do not understand.

+4
source share
2 answers

The background is set using the standard TextBox style. Based on the Priority value of the dependency property, Red is at # 11, and the default style is at # 9. The Blue option will be at # 3, so this should override the background.

You need to either explicitly set the background (for example, you use a blue brush) or create your own default style that does not set the background. Your default style may be based on the TextBox version.

+2
source

You can set Background in the style that applies to your MyTextBox :

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

As stated in CodeNaked, the default value for metadata is overridden by default for the text field. You can see this if you change the 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 the breakpoint is broken, you can see that OldValue was Red , NewValue is White , and from the stack trace you can see that this is due to the default style being applied.

+2
source

Source: https://habr.com/ru/post/1345220/


All Articles