我为我的应用程序创建了一些自定义UserControl,如果未启用该控件,我想设置背景颜色。
我见过几个使用<Style.Trigger>
的线程,但这不起作用。
以下是我的其中一个控件的来源:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="IFCSControls.SubMenuPrint"
x:Name="SubMenuPrintButton"
d:DesignWidth="640" d:DesignHeight="480" Width="25" Height="25">
<UserControl.Resources>
<Storyboard x:Key="MouseOver">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="path">
<EasingColorKeyFrame KeyTime="0" Value="#FF929292"/>
<EasingColorKeyFrame KeyTime="0:0:0.05" Value="#FF7CBDD8"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="MouseOut">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="path">
<EasingColorKeyFrame KeyTime="0" Value="#FF7CBDD8"/>
<EasingColorKeyFrame KeyTime="0:0:0.05" Value="#FF929292"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="LMBDown">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="path">
<EasingColorKeyFrame KeyTime="0" Value="#FF7CBDD8"/>
<EasingColorKeyFrame KeyTime="0:0:0.05" Value="#FF00B5FF"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="LMBUp">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="path">
<EasingColorKeyFrame KeyTime="0" Value="#FF00B5FF"/>
<EasingColorKeyFrame KeyTime="0:0:0.05" Value="#FF7CBDD8"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<UserControl.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<BeginStoryboard Storyboard="{StaticResource MouseOver}"/>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
<BeginStoryboard Storyboard="{StaticResource MouseOut}"/>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeftButtonDown">
<BeginStoryboard Storyboard="{StaticResource LMBDown}"/>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeftButtonUp">
<BeginStoryboard Storyboard="{StaticResource LMBUp}"/>
</EventTrigger>
</UserControl.Triggers>
<Grid x:Name="LayoutRoot">
<Rectangle Fill="#FF666666" Margin="0,0,0,0.013" Stroke="Black" StrokeThickness="0"/>
<Path x:Name="path" Data="M40.039037,46.667008 L40.039037,74.28701 C40.039037,74.28701 67.66371,74.28701 71.450127,74.28701 75.23661,74.28701 74.361614,71.375458 74.361614,71.375458 74.361614,71.375458 74.361614,52.938088 74.361614,49.990063 74.361614,47.042042 71.038874,46.667008 71.038874,46.667008 z M9.0112228,46.667008 C9.0112228,46.667008 5.6854935,47.042042 5.6854935,49.990067 5.6854935,52.938091 5.6854935,71.375458 5.6854935,71.375458 5.6854935,71.375458 4.8097115,74.28701 8.5995998,74.28701 12.389425,74.28701 40.038958,74.28701 40.038958,74.28701 L40.038958,46.667008 z M25.307034,30.218752 C26.784927,30.218752 39.628404,30.218752 40.029402,30.218752 L40.038983,30.218752 40.038983,36.531223 24.962935,36.531223 C24.962935,36.531223 24.024576,36.624981 24.024576,35.562477 24.024576,34.499973 24.024576,31.437496 24.024576,31.437496 24.024576,31.437496 23.805683,30.218752 25.307034,30.218752 z M40.039012,30.21875 L40.048584,30.21875 C40.449222,30.21875 53.281151,30.21875 54.757715,30.21875 56.257717,30.21875 56.039021,31.437494 56.039021,31.437494 56.039021,31.437494 56.039021,34.499973 56.039021,35.562477 56.039021,36.624981 55.101504,36.531223 55.101504,36.531223 L40.039012,36.531223 z M40.039012,19.656239 L40.048403,19.656239 C40.441516,19.656239 53.034081,19.656239 54.695231,19.656239 56.38275,19.656239 56.039021,20.937498 56.039021,20.937498 56.039021,20.937498 56.039021,23.999975 56.039021,24.937479 56.039021,25.874983 55.132747,25.999981 55.132746,25.999981 L40.039012,25.999981 z M25.369575,19.656239 C27.032221,19.656239 39.636117,19.656239 40.029583,19.656239 L40.038983,19.656239 40.038983,25.999983 24.931665,25.999983 C24.931665,25.999983 24.024576,25.874985 24.024576,24.937481 24.024576,23.999975 24.024576,20.937498 24.024576,20.937498 24.024576,20.937498 23.680538,19.656239 25.369575,19.656239 z M40.039012,9.4062387 L40.049126,9.4062387 C40.472377,9.4062387 54.024806,9.4062387 55.101504,9.4062387 56.195232,9.4062387 56.179611,10.343743 56.179611,10.343744 56.179611,10.343743 56.179611,13.374995 56.179611,14.359364 56.179611,15.343744 55.226474,15.312488 55.226474,15.312488 L40.039012,15.312488 z M24.962935,9.4062387 C26.040603,9.4062387 39.605228,9.4062387 40.02886,9.4062387 L40.038983,9.4062387 40.038983,15.312488 24.837853,15.312488 C24.837853,15.312488 23.883858,15.343744 23.883858,14.359364 23.883858,13.374996 23.883858,10.343744 23.883858,10.343744 23.883858,10.343744 23.868223,9.4062387 24.962935,9.4062387 z M40.039012,1.1927187E-06 L64.372036,1.1927187E-06 64.382071,0.00045084767 C64.480513,0.005978772 65.26804,0.075826826 65.26804,0.89599533 65.26804,1.7708251 65.26804,39.655991 65.26804,39.655991 65.26804,39.655991 65.268284,41.025982 66.63809,41.025982 68.007895,41.025982 78.133232,41.025982 78.133228,41.025982 78.133232,41.025982 79.810856,41.025742 80.020561,42.598698 L80.038982,42.88749 80.038982,80 40.039012,80 40.039012,40.811985 40.051685,40.811985 C40.581866,40.811985 57.556053,40.811985 58.632753,40.811985 59.72648,40.811985 59.50699,39.999512 59.50699,39.999512 59.50699,39.999512 59.50699,7.9992043 59.50699,6.4999697 59.50699,5.0007197 57.944992,4.9379918 57.944992,4.9379923 L40.039012,4.9687454 40.039012,4.9374997 z M15.684061,1.1927187E-06 L40.038983,1.1927187E-06 40.038983,4.9374997 40.038983,4.9687454 22.11689,4.9379923 C22.11689,4.9379918 20.553484,5.0007197 20.553484,6.4999697 20.553484,7.9992048 20.553484,39.999512 20.553484,39.999512 20.553484,39.999512 20.333797,40.811985 21.428511,40.811985 22.506178,40.811985 39.49564,40.811985 40.026298,40.811985 L40.038983,40.811985 40.038983,80 0.0030136108,80 0.0030136108,42.88749 0.021453857,42.598698 C0.23134613,41.025742 1.9104805,41.025982 1.9104805,41.025982 1.9104805,41.025982 12.044931,41.025982 13.415968,41.025982 14.787006,41.025982 14.787251,39.655991 14.787251,39.655991 14.787251,39.655991 14.787251,1.7708251 14.787251,0.89599533 14.787251,0.075826826 15.575487,0.0059787725 15.674017,0.00045084767 z M64.372009,0 L80.041996,0 C80.041996,1.8865438E-07 80.041996,41.408734 80.041996,42.934731 L80.038982,42.88749 80.038982,1.1927187E-06 z M0,0 L15.684088,0 0.0030136108,1.1927187E-06 0.0030136108,42.88749 0,42.934731 C0,41.408734 0,1.8865438E-07 0,0 z" Fill="#FF929292" Margin="0,0,0,0.013" Stretch="Fill" Stroke="Black" StrokeThickness="0" RenderTransformOrigin="0.5,-3.028"/>
</Grid>
如果有人可以帮助我,那会很好:) 提前谢谢!
答案 0 :(得分:0)
以下是我在启用或禁用用户控件时更改背景的代码。
我所做的是将Style(RectangleStyle1)和触发器添加到Rectangle。我删除了从声明中设置矩形的Fill,因为它将覆盖Style尝试进行的任何更改。
将此样式添加到UserControl.Resources
<Style x:Key="RectangleStyle1" TargetType="{x:Type Rectangle}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Fill" Value="#FF666666" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Fill" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
用
替换矩形声明<Rectangle x:Name="rectangle"
Margin="0,0,0,0.013"
Stroke="Black"
StrokeThickness="0" Style="{DynamicResource RectangleStyle1}" />
如果矩形仅用于背景,则可以删除矩形并使网格(LayoutRoot)不透明,并使用背景而不是填充来设置它。