8

I am trying to style a WPF xctk:ColorPicker. I want to change the background color of the dropdown view and text without redefining the whole style.

I know that the ColorPicker contains e.g. a part named "PART_ColorPickerPalettePopup". Is there a way that I can directly reference this part in my style, providing e.g. a new Background color only?

I want to avoid having to redefine all the other properties of "PART_ColorPickerPalettePopup".

Link to the ColorPicker I am describing

4

2 回答 2

9

您可以将 Style 基于另一个 Style 并覆盖特定的 setter:

<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>

但是您不能仅“覆盖” ControlTemplate 的一部分。不幸的是,您必须(重新)将整个模板定义为一个整体。

于 2017-01-10T13:38:51.343 回答
5

通过 VisualTreeHelper 从 ColorPicker 获取弹出窗口并更改边框属性(弹出窗口的子项),如下所示:

   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;
    }
于 2017-01-10T16:01:12.973 回答