3kings
3kings

Reputation: 838

WPF Combobox Setting Visual Style Stroke

So I made my own class called ComboBoxUltra and it inherits from ComboBox in System.Windows.Controls.

I am re-templating it and need help on how can I set the Stroke value on the Rectangle of the FocusVisualStyle to a new DependencyProperty with in the ComboBoxUltra.

This is what I have and it does not work. I have the other stuff defined if need be to post it on here... But its quite lengthy.

In Xaml I have:

<Style x:Key="ComboBoxFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle SnapsToDevicePixels="true"
                               Stroke="{Binding RelativeSource={RelativeSource AncestorType={x:Type controls:ComboBoxPro}, AncestorLevel=1}, Path=FocusedBorderBrush}"
                               StrokeThickness="1" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

<Style TargetType="{x:Type controls:ComboBoxUltra}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ComboBoxFocusVisual}" />
        <Setter Property="Background" Value="{StaticResource ComboBox.Static.Background}" />
        <Setter Property="BorderBrush" Value="{StaticResource ComboBox.Static.Border}" />
        <Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.WindowTextBrushKey}}" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="Padding" Value="6,3,5,3" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="true" />
        <Setter Property="ScrollViewer.PanningMode" Value="Both" />
        <Setter Property="Stylus.IsFlicksEnabled" Value="False" />
        <Setter Property="Template" Value="{StaticResource ComboBoxTemplate}" />

        <Setter Property="FontSize" Value="{DynamicResource TextFontSizeNormal}" />
        <Style.Triggers>
            <Trigger Property="IsEditable" Value="true">
                <Setter Property="IsTabStop" Value="false" />
                <Setter Property="Padding" Value="2" />
                <Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}" />
            </Trigger>
        </Style.Triggers>
    </Style>

And in my ComboBoxUltra class I have the DependencyProperty as...

public static readonly DependencyProperty FocusedBorderBrushProperty =
            DependencyProperty.Register(nameof(FocusedBorderBrush), typeof(Brush), typeof(ComboBoxUltra),
                new FrameworkPropertyMetadata(Brushes.Orange, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
        public Brush FocusedBorderBrush
        {
            get => (Brush)GetValue(FocusedBorderBrushProperty);
            set => SetValue(FocusedBorderBrushProperty, value);
        }

I've tried removing the FocusVisualStyle And just do a Trigger on IsKeyboardFocusWithin, and set the border brush with a setter but that did not work.

Any help is appreciated.

Upvotes: 0

Views: 366

Answers (1)

mm8
mm8

Reputation: 169160

You can't bind to a property of the ComboBox in the FocusVisualStyle since it is displayed on the adorned layer.

The trigger approach that you have tried is the correct one. Just remember to set the FocusVisualStyle property to null, or set the Stroke property of the Rectangle to Transparent to get rid of it.

Upvotes: 1

Related Questions