2010-09-22 5 views
1

J'ai une ligne WPF personnalisée, que je veux changer de style lorsque je sélectionne avec la souris.WPF Trigger ne fonctionne pas (exemple)

J'ai essayé d'utiliser des déclencheurs (voir code ci-dessous), mais cela ne fonctionne pas.

Où est le problème? La IsSelected propriété est modifiée, mais le StrokeThickness ne change pas du tout ... (

XAML:

<UserControl.Resources> 
<Style TargetType="stops:MyLine" 
    x:Key="MyLineStyleKey" x:Name="MyLineStyleName"> 

    <Style.Triggers> 
    <DataTrigger Binding="{Binding Path=IsSelected}" Value="true"> 
    <Setter Property="StrokeThickness" Value="10" /> 
    </DataTrigger> 
    </Style.Triggers> 
</Style> 
</UserControl.Resources> 

<Canvas... 
... 
<my:MyLine Style="{StaticResource MyLineStyleKey}" x:Name="myLine1" 
Stroke="Black" X11="10" X22="100" Y11="10" Y22="100"/> 

code:

class MyLine : Shape 
{ 
    public static readonly DependencyProperty X11Property; 
    public static readonly DependencyProperty X22Property; 
    public static readonly DependencyProperty Y11Property; 
    public static readonly DependencyProperty Y22Property; 
    public static readonly DependencyProperty IsSelectedProperty; 

    static MyLine() 
    { 
     X11Property = DependencyProperty.Register("X11", typeof(double), typeof(MyLine), new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.AffectsRender)); 
     X22Property = DependencyProperty.Register("X22", typeof(double), typeof(MyLine), new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.AffectsRender)); 
     Y11Property = DependencyProperty.Register("Y11", typeof(double), typeof(MyLine), new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.AffectsRender)); 
     Y22Property = DependencyProperty.Register("Y22", typeof(double), typeof(MyLine), new FrameworkPropertyMetadata(double.NaN, FrameworkPropertyMetadataOptions.AffectsRender)); 
     IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(MyLine), new FrameworkPropertyMetadata(false)); 
    } 

    public MyLine() 
     : base() 
    { 
     this.PreviewMouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(DirectionLine_PreviewMouseLeftButtonDown); 
    } 

    void DirectionLine_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) 
    { 
     this.IsSelected = !this.IsSelected; 
    } 

    [TypeConverter(typeof(LengthConverter))] 
    public double X11 { get { return (double)GetValue(X11Property); } set { SetValue(X11Property, value); } } 
    [TypeConverter(typeof(LengthConverter))] 
    public double X22 { get { return (double)GetValue(X22Property); } set { SetValue(X22Property, value); } } 
    [TypeConverter(typeof(LengthConverter))] 
    public double Y11 { get { return (double)GetValue(Y11Property); } set { SetValue(Y11Property, value); } } 
    [TypeConverter(typeof(LengthConverter))] 
    public double Y22 { get { return (double)GetValue(Y22Property); } set { SetValue(Y22Property, value); } } 
    [TypeConverter(typeof(BooleanConverter))] 
    public bool IsSelected { get { return (bool)GetValue(IsSelectedProperty); } set { SetValue(IsSelectedProperty, value); } } 


    protected override System.Windows.Media.Geometry DefiningGeometry 
    { 
     get 
     { 
      var geometryGroup = new GeometryGroup(); 
      geometryGroup.Children.Add(new LineGeometry(new Point(X11, Y11), new Point(X22, Y22))); 
      return geometryGroup; 
     } 
    } 
} 

Répondre

2

Essayez d'utiliser "<Trigger Property="IsSelected" ...>" au lieu d'un DataTrigger. DataTrigger fonctionne sur le DataContext pour un FrameworkElement, ce qui signifie qu'il recherchera la propriété IsSelected du contrôle DataContext du contrôle, tandis que Trigger fonctionnera directement sur FrameworkElement lui-même. Propriétés d'endurance de FrameworkElement.

<UserControl x:Class="StackOverflow.MyUserControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:StackOverflow"> 
    <UserControl.Resources> 
     <Style TargetType="local:MyLine" 
    x:Key="MyLineStyleKey" x:Name="MyLineStyleName"> 

      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="true"> 
        <Setter Property="StrokeThickness" Value="10" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </UserControl.Resources> 
    <Canvas> 
     <local:MyLine Style="{StaticResource MyLineStyleKey}" x:Name="myLine1" 
Stroke="Black" X11="10" X22="100" Y11="10" Y22="100"/> 
    </Canvas> 
</UserControl> 
+0

' \t ' - le même résultat ... – serhio

+0

Dois-je ajouter le nœud Tigger dans l'élément MyLine plutôt que dans UserControl cancvas Ressources? – serhio

+0

Je l'ai essayé et ça marche. Je poste mon code. – ASanch