WPF: Is there a way to override the ControlTemplate part without overriding the whole style?

I am trying to create WPF xctk: ColorPicker. I want to change the background color of the drop-down list and the text without , redefining the whole style.

I know that ColorPicker contains, for example, a part called "PART_ColorPickerPalettePopup". Is there a way that I can directly refer to this part in my style by providing, for example, a new background color only ?

I want to avoid overriding all other properties of PART_ColorPickerPalettePopup.

The ColorPicker link I'm talking about

+4
source share
2 answers

You can create a style for another style and override special setters:

<Style x:Key="myStyle" TargetType="xctk:ColorPicker" BasedOn="{StaticResource {x:Type xctk:ColorPicker}}">
    <!-- This will override the Background setter of the base style -->
    <Setter Property="Background" Value="Red" />
</Style>

But you cannot "override" only a part of the ControlTemplate. Unfortunately, then you must (re) define the whole template as a whole.

+6
source

Get a popup from ColorPicker using VisualTreeHelper and change the border properties (popup child) as follows:

   private void colorPicker_Loaded(object sender,RoutedEventArgs e)
    {
        Popup popup = FindVisualChildByName<Popup> ((sender as DependencyObject),"PART_ColorPickerPalettePopup");
        Border border = FindVisualChildByName<Border> (popup.Child,"DropDownBorder");
        border.Background = Brushes.Yellow;
    }

    private T FindVisualChildByName<T>(DependencyObject parent,string name) where T:DependencyObject
    {
        for (int i = 0;i < VisualTreeHelper.GetChildrenCount (parent);i++)
        {
            var child = VisualTreeHelper.GetChild (parent,i);
            string controlName = child.GetValue (Control.NameProperty) as string;
            if (controlName == name)
            {
                return child as T;
            }
            else
            {
                T result = FindVisualChildByName<T> (child,name);
                if (result != null)
                    return result;
            }
        }
        return null;
    }
+3
source

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


All Articles