Vítor

Hello friends!

I am trying to define a Style for FocusVisualStyle property like this:

<Style x:Key="RibbonTabItemFocusVisual" TargetType="{x:Type awc:RibbonTabItem}">

<Setter Property="Background" Value="Orange" />

</Style>

So I am receiving the following error:

'RibbonTabItem' targetType does not match type of element 'Control'.

But it match, RibbonTabItem inherits TabItem class.

Excuse me for font sizes, this editor is terrible.

Thanks.




Re: Windows Presentation Foundation (WPF) Custom Control FocusVisualStyle Template

Dennis Cheng-MSFT

Try settings TargetType to the type of the base class, Control. We take a closer look at this because it could be a bug.




Re: Windows Presentation Foundation (WPF) Custom Control FocusVisualStyle Template

Vitor

Hello Dennis

I already was tried to set to Control, so the Style is not applied like my control is not a control inheritor.

Thanks.

Vitor






Re: Windows Presentation Foundation (WPF) Custom Control FocusVisualStyle Template

Dennis Cheng - MSFT

Hi, this is not a bug after taking a closer look at how FocusVisualStyle is used in WPF. The TargetType of your FocusVisualStyle should be "{x:Type Control}" and not the type of the element the FocusVisualStyle property is being set on. This is because WPF internally applies your FocusVisualStyle on Control which is used in a Adorner when the FocusVisual is shown.




Re: Windows Presentation Foundation (WPF) Custom Control FocusVisualStyle Template

Vitor

Much very thanks Dennis.

I also noted that WPF just get the Template property; if I set a Background property, for example, has no effect.

So, will never possible to set a ContentPresenter on a FocusVisualStyle (because ContentPresenter it's a ContentControl property)

I want to do is a FocusVisualStyle like Office 2007 Ribbon, then I will need a ContentPresenter because template replaces background (orange).

Thanks






Re: Windows Presentation Foundation (WPF) Custom Control FocusVisualStyle Template

Dennis Cheng-MSFT

The FocusVisual is drawn on top of your Control in the Adorner Layer which is separate from the Control itself. You can try setting the background and opacity on the ControlTemplate of the FocusVisualStyle like this to get a similar look:

<Page
  xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <Style x:Key="MyFocusVisual">
      <Setter Property="Control.Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type Control}">
                <Border Background="Orange" Opacity=".3" CornerRadius="4">
                    <Rectangle Margin="2" StrokeThickness="1" Stroke="Black" StrokeDashArray="1 2"/>
                </Border>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Page.Resources>

  <StackPanel Orientation="Horizontal">
   <Button FocusVisualStyle="{DynamicResource MyFocusVisual}">Focus Here</Button>
   <Button FocusVisualStyle="{DynamicResource MyFocusVisual}">Focus Here</Button>
  </StackPanel>

</Page>

 






Re: Windows Presentation Foundation (WPF) Custom Control FocusVisualStyle Template

Vitor

Mutch very thanks Dennis!