2011-09-29 52 views
1

我有一個包含的膨脹機的用戶控件設置背景:XAML用戶控件 - 基於觸發器

<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> 

答案基本上所有我想要做的是改變基礎上的的IsExpanded的擴展和StackPanel中的背景顏色UserControl(或擴展器)。

我已經添加了三個扶養屬性來控制:

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)); 

但我的代碼不能正常工作。從usercontrol的IsExpanded屬性確實工作,因爲當從放置usercontrol的窗口中檢查屬性時,屬性會相應地發生變化(當擴展器展開時)。

如何根據UserControl.IsExpanded屬性更改擴展器的背景顏色?

謝謝!

編輯: 我在同時做了以下:

<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> 

並刪除了BackgroundProperty扶養財產。我實際上認爲這可以工作,但唉...

回答

1

我設法解決我的問題。隨函附上的解決方案(只顯示必要的代碼)...

我扶養物業創建如下:

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

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

,我的XAML是:

<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> 

最終的解決方案是從元素中移除Backgroud屬性併爲IsExpanded = True和IsExpanded = False指定一個DataTrigger。因此,在元素屬性中指定時,它看起來像Background屬性會覆蓋觸發器試圖設置的任何內容。

希望這可以幫助別人!

0

。由此另一個解決同樣的問題,使用的IValueConverter這個時候......

我的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> 

和我的價值轉換器代碼:

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; 
    } 
} 

我儘管我認爲我更喜歡僅XAML解決方案,儘管IValueConverter選項確實可以使XAML的讀取效果稍好一些...

(DependancyProperty仍以完全相同的方式創建)