XAML用户控件 - 基于触发器

XAML用户控件 - 基于触发器

问题描述:

我有一个包含的膨胀机的用户控件设置背景: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扶养财产。我实际上认为这可以工作,但唉...

我设法解决我的问题。随函附上的解决方案(只显示必要的代码)...

我扶养物业创建如下:

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属性会覆盖触发器试图设置的任何内容。

希望这可以帮助别人!

。由此另一个解决同样的问题,使用的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仍以完全相同的方式创建)