Savad
Savad

Reputation: 1

WPF - Same property setting from CustomControl's generic xaml and In place where it used not overriding

Screenshots:

I have a custom control with Style as

<Style TargetType="{x:Type local:IconCustom}">      
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type local:IconCustom}">
           <Border x:Name="border" Background="{TemplateBinding Background}"                             BorderBrush="{TemplateBinding BorderBrush}"                             BorderThickness="{TemplateBinding BorderThickness}">
             <Border.Style>
               <Style TargetType="Border">
                 <Style.Triggers>
                   <Trigger Property="IsMouseOver" Value="True">
                     <Setter Property="Background" Value="Red">
                   </Trigger>
                 </Style.Triggers>
               </Style>
             </Border.Style>
             <TextBlock Text="Abcde"/>
           </Border>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style> 

the custom control is place in CustomTest

<uc:IconCustom Width="400" Height="400" Style="{StaticResource IconButtonStyle}"/> 

with a Style

<Style TargetType="{x:Type uc:IconCustom}" x:Key="IconButtonStyle">
   <Style.Triggers>
       <Trigger Property="IsMouseOver" Value="True">
          <Setter Property="Background" Value="Yellow"/>
       </Trigger>
   </Style.Triggers>
</Style> 

here Yellow is not overriding on Red for MouseOver, why this happens, How to fix?

Tried setting Default style for MouseOver, and Style from CustomTest, it also not worked, I want if no style is given in CustomTest, then Default style (Red) will take, otherwise if Style applied in CustomTest, then Yellow will take,

Upvotes: 0

Views: 87

Answers (1)

user3843952
user3843952

Reputation:

you have two problems:

  1. You set the red background on a border in a Template. You can't access this border in your IconButtonStyle.
  2. IconButtonStyle overrides the whole style, also the Template. You won't see your text.

Possible solution:

  1. Don't set the background on the border, set the trigger directly for IconCustom.
  2. Add BasedOn="{StaticResource{x:Type local:IconCustom}}" to IconButtonStyle, so you will just override the trigger but keep the template.

The code:

<Style TargetType="{x:Type local:IconCustom}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:IconCustom}">
                <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <TextBlock Text="Abcde"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>

<Style TargetType="{x:Type uc:IconCustom}" x:Key="IconButtonStyle" BasedOn="{StaticResource {x:Type uc:IconCustom}}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Yellow" />
        </Trigger>
    </Style.Triggers>
</Style>

Default now is red, and with IconButtonStyle it's yellow.

Upvotes: 0

Related Questions