【问题标题】:WPF button mouseover or hover change rectangle background [closed]WPF按钮鼠标悬停或悬停更改矩形背景[关闭]
【发布时间】:2015-01-14 18:00:20
【问题描述】:

当光标位于指定按钮上时,我想更改矩形(或其他)的背景/图像源或可见性。

【问题讨论】:

  • 太棒了!你试过什么吗? EventTrigger 或许?
  • 发新帖前需要搜索类似问题。
  • @Abdullah 如果你找到了请出示
  • @BradleyDotNET 是的,我试过了,在这个窗口中为我的按钮创建了一个自定义样式,但是我有四个按钮,当鼠标悬停在它们上方时,每个按钮都应该为矩形更改不同的背景。但是我不太擅长xaml,可能是我做错了。
  • 我复制了你的问题标题,粘贴到google,找到this post

标签: c# wpf xaml mouseover


【解决方案1】:

当鼠标悬停在按钮上时,此代码会更改矩形背景。

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="400" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="200"></RowDefinition>
            <RowDefinition Height="200"></RowDefinition>
        </Grid.RowDefinitions>
        <Button Width="200" Height="100" Content="Hover over me..." x:Name="btn">           
        </Button>
        <Rectangle Height="200" Grid.Row="1" x:Name="Rect1">
            <Rectangle.Style>
                <Style TargetType="Rectangle">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=btn, Path=IsMouseOver}" Value="True">
                            <Setter Property="Fill" Value="Blue" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Rectangle.Style>            
        </Rectangle>
    </Grid>    
</Window>

【讨论】:

  • 谢谢,加上阿卜杜拉的提示,它正在工作!:)
猜你喜欢
  • 1970-01-01
  • 2017-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
相关资源
最近更新 更多