【问题标题】:Change the Grid Background color with EventTrigger on MouseDown使用 MouseDown 上的 EventTrigger 更改网格背景颜色
【发布时间】:2014-01-25 23:20:13
【问题描述】:

我想在 XAML EventTrigger MouseDown 中更改我的 UserContorl 中的网格背景 以下是我的代码:

<UserControl x:Class="OneCardApp.Ex.EntranceGuard.ControlManager.MapMonitoring.Map.MonitorDoor"
         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" Loaded="UserControl_Loaded" FontSize="10" MouseDown="UserControl_MouseDown" MouseMove="UserControl_MouseMove" MouseUp="UserControl_MouseUp">
<Grid Height="60" Width="50" Name="content">
    <Grid.Resources>
        <GradientStop x:Name="Stop2" Color="Blue" Offset="1" x:Key="s"></GradientStop>
        <Style TargetType="{x:Type Grid}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="blue"></Setter>
                </Trigger>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="red"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="36"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Image Name="img_door" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="0" Source="/OneCardApp.Ex;component/image/36/companyInfo.png"></Image>
    <Label Name="lbl_name" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1"></Label>
</Grid>

我尝试创建添加 IsPressed 触发器,但它没有。 所以,我该怎么做。 谢谢。

【问题讨论】:

  • 这听起来像是对这个问题的欺骗:stackoverflow.com/questions/10667545
  • 我想知道当鼠标左键单击带有 eventtrigger 或 tiggers 的网格内容时如何更改网格背景颜色。

标签: c# wpf eventtrigger


【解决方案1】:

试试这个

并根据您的要求选择颜色

     <Grid Height="60" Width="50" Name="content" Background="Transparent" Focusable="True">
    <Grid.Resources>
        <GradientStop x:Name="Stop2" Color="Blue" Offset="1" x:Key="s"></GradientStop>
        <Style TargetType="{x:Type Grid}">
            <Style.Triggers>
                <EventTrigger RoutedEvent="MouseEnter">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Background).(SolidColorBrush.Color)">
                                <EasingColorKeyFrame KeyTime="0" Value="Blue" />
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="GotKeyboardFocus">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Background).(SolidColorBrush.Color)">
                                <EasingColorKeyFrame KeyTime="0" Value="Red" />
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="MouseDown">
                    <BeginStoryboard>
                        <Storyboard >
                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.Background).(SolidColorBrush.Color)">
                                <EasingColorKeyFrame KeyTime="0" Value="Green" />
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="36"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Image Name="img_door" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="0" Source="/OneCardApp.Ex;component/image/36/companyInfo.png"></Image>
    <Label Name="lbl_name" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1"></Label>
</Grid>

【讨论】:

  • 谢谢,但代码不执行,他们无法解析 Background.Color 属性。
  • 单独运行这段代码它正在工作..你的网格尺寸很小。所以也许你没有发现..再试一次
  • 如果这个动画不起作用,则添加之前的动画
  • 我有更改代码..和以前的代码也可以工作。正如你所说的解决背景颜色属性错误我只是改变了编写动画的方式。如果它正在工作,请通知我。谢谢
  • 感谢您为我回答,我尝试再次更改代码,写入“(Grid.Background)。(SolidColorBrush.Color)”,但错误“背景”属性不直接指向路径“(0).(1)”依赖对象。为什么会出错。
猜你喜欢
  • 1970-01-01
  • 2023-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-02
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多