2011-09-29 2 views
1

J'ai un UserControl qui contient un Expander:XAML UserControl - tâche de fond basé sur Trigger

<UserControl x:Class="Client.DevicesExpander" 
      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" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300" 
      x:Name="devicesExpander" > 

    <Expander Margin="1" Name="expander" FontSize="11" BorderThickness="1" BorderBrush="DarkRed" Foreground="Black" Header="{Binding ElementName=devicesExpander, Path=Header}" FontWeight="Bold" MouseDoubleClick="Expander_MouseDoubleClick" Expanded="Expander_Expanded" IsExpanded="{Binding ElementName=devicesExpander, Path=IsExpanded}" Background="{Binding ElementName=devicesExpander, Path=Background}"> 
     <StackPanel Name="_devicesPanel"> 
      <ListBox BorderThickness="0,1,0,0" Name="_devicesList" FontWeight="Normal" MouseDoubleClick="DevicesList_MouseDoubleClick" Background="{Binding ElementName=devicesExpander, Path=Background}" /> 
     </StackPanel> 
     <Expander.Style> 
      <Style TargetType="{x:Type Expander}"> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding ElementName=devicesExpander, Path=IsExpanded}" Value="True"> 
         <Setter Property="Background" Value="White" /> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
      </Expander.Style> 
    </Expander> 
</UserControl> 

Ans fondamentalement tout ce que je voudrais faire est de changer l'extension et la couleur d'arrière-plan StackPanel sur la base IsExpanded du UserControl (ou Expander).

J'ai ajouté trois propriétés au contrôle dépendance:

public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(string), typeof(DevicesExpander)); 
    public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register("IsExpanded", typeof(bool), typeof(DevicesExpander)); 
    public static readonly DependencyProperty BackgroundProperty = DependencyProperty.Register("Background", typeof(System.Windows.Media.Brush), typeof(DevicesExpander)); 

Mais mon code ne fonctionne pas. La propriété IsExpanded de la commande usercontrol fonctionne comme la propriété change en conséquence (lorsque l'expandeur se développe) lorsqu'elle est vérifiée à partir de la fenêtre dans laquelle la commande usercontrol est placée.

Comment puis-je modifier la couleur d'arrière-plan de l'Expander en fonction de la propriété UserControl.IsExpanded?

Merci!

EDIT: Je ai entre-temps fait ce qui suit:

<UserControl.Resources> 
     <Style TargetType="{x:Type UserControl}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding ElementName=devicesExpander, Path=IsExpanded}" Value="True"> 
        <Setter Property="Background" Value="White" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </UserControl.Resources> 

    <Expander Margin="1" Name="expander" FontSize="11" BorderThickness="1" BorderBrush="DarkRed" Foreground="Black" Header="{Binding ElementName=devicesExpander, Path=Header}" FontWeight="Bold" MouseDoubleClick="Expander_MouseDoubleClick" Expanded="Expander_Expanded" IsExpanded="{Binding ElementName=devicesExpander, Path=IsExpanded}" Background="Transparent"> 
     <StackPanel Name="_devicesPanel"> 
      <ListBox BorderThickness="0,1,0,0" Name="_devicesList" FontWeight="Normal" MouseDoubleClick="DevicesList_MouseDoubleClick" Background="Transparent" /> 
     </StackPanel> 
    </Expander> 

et supprimé la propriété BackgroundProperty de dépendance. Je pensais vraiment que cela pourrait fonctionner, mais hélas ...

Répondre

1

J'ai réussi à résoudre mon problème. Ci-joint la solution (ne montrant que le code essentiel) ...

Mon propriété est créée Dépendance comme suit:

public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register("IsExpanded", typeof(bool), typeof(DevicesExpander)); 

public bool IsExpanded 
{ 
    get { return (bool)GetValue(IsExpandedProperty); } 
    set { SetValue(IsExpandedProperty, value); } 
} 

et mon XAML est:

<UserControl x:Class="TestApp.DevicesExpander" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Name="devicesExpander"> 

    <Expander> 
     <Expander.Style> 
      <Style TargetType="Expander"> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding ElementName=devicesExpander, Path=IsExpanded}" Value="True"> 
         <Setter Property="Background" Value="Black" /> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding ElementName=devicesExpander, Path=IsExpanded}" Value="False"> 
         <Setter Property="Background" Value="Red" /> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </Expander.Style> 
    </Expander> 
</UserControl> 

La solution à la fin était pour supprimer la propriété Backgroud de l'élément et spécifier un DataTrigger pour IsExpanded = True et IsExpanded = False. Il semble donc que la propriété Background, lorsqu'elle est spécifiée dans les propriétés de l'élément, remplace tout ce que les triggers ont tenté de définir.

Espérons que cela aide quelqu'un!

0

une autre solution à la présente, le même problème, cette fois en utilisant un IValueConverter ...

Mon XAML:

<UserControl.Resources> 
     <local:BackgroundConverter x:Key="backgroundConvertor" /> 
    </UserControl.Resources> 

    <Expander> 
     <Expander.Style> 
      <Style TargetType="Expander"> 
       <Setter Property="Background" Value="{Binding ElementName=devicesEntry, Path=IsExpanded, Converter={StaticResource backgroundConvertor}}" /> 
      </Style> 
     </Expander.Style> 
    </Expander> 

et mon code pour la valeur convertisseur:

public class BackgroundConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return (bool)value ? "White" : "Transparent"; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     string color = value as string; 

     if (color == "White") 
      return true; 

     return false; 
    } 
} 

I Je pense que je préfère la solution XAML seulement, bien que l'option IValueConverter permette une lecture légèrement meilleure du XAML ...

(Le DependancyProperty est toujours créé exactement de la même manière)

Questions connexes