2016-01-20 5 views
0

J'ai créé un contrôle WPF personnalisé et le style ne semble pas fonctionner. J'ai essayé de créer des styles externes et des styles internes et aucun ne semble fonctionner. Voici le code pour le style interne.Personnalisation d'un contrôle WPF personnalisé non fonctionnel

contrôle personnalisé

public class NavigationTextBlock : TextBlock 
{ 
    static NavigationTextBlock() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(
      typeof(NavigationTextBlock), 
      new FrameworkPropertyMetadata(typeof(NavigationTextBlock))); 
    } 

    public static readonly DependencyProperty IsSelectedProperty = 
     DependencyProperty.Register("IsSelected", typeof(bool), typeof(NavigationTextBlock), new UIPropertyMetadata(false)); 

    public bool IsSelected 
    { 
     get 
     { 
      return (bool)GetValue(IsSelectedProperty); 
     } 
     set 
     { 
      SetValue(IsSelectedProperty, value); 
     } 
    } 
} 

XAML

Aucun le style sous <c:NavigationTextBlock.Styles> sont appliquées.

<UserControl x:Class="Fallout4Checklist.Views.NavigationView" 
     xmlns:metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:Fallout4Checklist.Views" 
     xmlns:c="clr-namespace:Fallout4Checklist.Controls" 
     xmlns:cal="http://www.caliburnproject.org" 
     mc:Ignorable="d" 
     d:DesignHeight="100" 
     d:DesignWidth="800"> 
<Border Padding="16, 0, 16, 0"> 
    <ItemsControl ItemsSource="{Binding NavigationItems}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid Rows="1" /> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <c:NavigationTextBlock 
       FontFamily="Segoe UI" 
       FontSize="24" 
       FontWeight="SemiBold" 
       Foreground="DarkGray" 
       VerticalAlignment="Center" 
       TextAlignment="{Binding TextAlignment}" 
       Text="{Binding Content}"> 
        <c:NavigationTextBlock.Style> 
         <Style TargetType="{x:Type c:NavigationTextBlock}"> 
          <Style.Triggers> 
           <DataTrigger Binding="{Binding IsMouseOver}" Value="True"> 
            <Setter Property="Foreground" Value="#CC119EDA" /> 
           </DataTrigger> 
           <DataTrigger Binding="{Binding IsSelected}" Value="False"> 
            <Setter Property="Foreground" Value="DarkGray" /> 
           </DataTrigger> 
           <MultiDataTrigger> 
            <MultiDataTrigger.Conditions> 
             <Condition Binding="{Binding IsSelected}" Value="True" /> 
             <Condition Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsMouseOver}" Value="True" /> 
            </MultiDataTrigger.Conditions> 
            <Setter Property="Foreground" Value="#0d78a6" /> 
           </MultiDataTrigger> 
           <MultiDataTrigger> 
            <MultiDataTrigger.Conditions> 
             <Condition Binding="{Binding IsSelected}" Value="False" /> 
             <Condition Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsMouseOver}" Value="True" /> 
            </MultiDataTrigger.Conditions> 
            <Setter Property="Foreground" Value="#8c8c8c" /> 
           </MultiDataTrigger> 
          </Style.Triggers> 
         </Style> 
        </c:NavigationTextBlock.Style> 
       </c:NavigationTextBlock> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</Border> 
</UserControl> 

Répondre

1

Vous définissez les propriétés directement sur le contrôle, qui remplacent le style.

Supprimer

Foreground="DarkGray" 
+0

Wow. Je ne suis pas intelligent. Merci. – Jeff