【问题标题】:Background of disabled control in wpfwpf中禁用控件的背景
【发布时间】:2014-12-09 09:41:18
【问题描述】:

我创建了自定义按钮用户控件。它必须在触摸屏上工作,所以我必须在 IsClicked 触发器上使用动画而不是设置属性。它工作正常,但是当 UserControl IsEnabled 属性设置为 false 时,我还必须使边框的灰色背景渐变。

这是我的代码:

<UserControl x:Class="TicketApplication.Controls.RectangleButton"
         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"
         x:Name="button"
         Width="255"
         Height="85"
         mc:Ignorable="d">
<Grid x:Name="grid"
      HorizontalAlignment="Stretch"
      VerticalAlignment="Stretch">
    <Border x:Name="border"
            Width="Auto"
            Height="Auto"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            CornerRadius="20">
        <Border.Background>
            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Offset="0.138" Color="{Binding Path=BackgroundGradientTopColor}" />
                <GradientStop Offset="0.982" Color="{Binding Path=BackgroundGradientBottomColor}" />
            </LinearGradientBrush>
        </Border.Background>
        <TextBlock x:Name="text"
                   Width="Auto"
                   Height="Auto"
                   Margin="0"
                   HorizontalAlignment="Stretch"
                   VerticalAlignment="Center"
                   FontFamily="Open Sans"
                   FontSize="{Binding Path=TextSize,
                                      FallbackValue=40}"
                   FontWeight="Bold"
                   Foreground="White"
                   ScrollViewer.VerticalScrollBarVisibility="Disabled"
                   Text="{Binding Path=Text,
                                  FallbackValue='Test'}"
                   TextAlignment="Center"
                   TextWrapping="Wrap" />
        <Border.Style>
            <Style TargetType="{x:Type Border}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsEnabled}" Value="false">
                        <Setter Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                    <GradientStop Offset="0.138" Color="#3d4144" />
                                    <GradientStop Offset="0.982" Color="#3d4144" />
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
    </Border>
    <Grid.Triggers>
        <EventTrigger RoutedEvent="Grid.MouseDown">
            <BeginStoryboard>
                <Storyboard Completed="Storyboard_Completed">
                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)">
                        <EasingColorKeyFrame KeyTime="0:0:0:0.0" Value="#3d4144" />
                        <EasingColorKeyFrame KeyTime="0:0:0:0.15" Value="{Binding Path=BackgroundGradientTopColor}" />
                    </ColorAnimationUsingKeyFrames>
                    <ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Border.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)">
                        <EasingColorKeyFrame KeyTime="0:0:0:0.0" Value="#3d4144" />
                        <EasingColorKeyFrame KeyTime="0:0:0:0.15" Value="{Binding Path=BackgroundGradientBottomColor}" />
                    </ColorAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Grid.Triggers>
</Grid>

如您所见,我为 IsEnabled 属性设置了触发器,但它不起作用。

【问题讨论】:

    标签: c# wpf triggers


    【解决方案1】:

    LocalValue 的优先级高于来自触发器的值。欲了解更多信息,请访问此link。为了解决您的问题,不要直接设置默认背景,而是通过样式设置器进行设置。以下是修改后的边框样式。

    <Style TargetType="{x:Type Border}">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0.5,0"
                                        EndPoint="0.5,1">
                    <GradientStop Offset="0.138"
                                    Color="{Binding Path=BackgroundGradientTopColor}" />
                    <GradientStop Offset="0.982"
                                    Color="{Binding Path=BackgroundGradientBottomColor}" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsEnabled}"
                            Value="false">
                <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush StartPoint="0.5,0"
                                                EndPoint="0.5,1">
                            <GradientStop Offset="0.138"
                                            Color="#3d4144" />
                            <GradientStop Offset="0.982"
                                            Color="#3d4144" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    

    【讨论】:

    • 太好了,它有效,谢谢!您知道如何在此类控件中实现 IsCheckable 属性吗?我不知道如何省略 块由于跳过动画,假设我在后面的代码中有属性 IsCheckable?
    猜你喜欢
    • 2013-10-13
    • 2014-10-13
    • 2019-04-07
    • 2016-01-05
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多