如何修复 ToggleButton 悬停检测?
How to fix ToggleButton hover detection?
当我将鼠标悬停在按钮上时,它会变成浅绿色,但是一旦移开光标它就不会切换回黑色。
我是 C# 的新手,正在尝试掌握基本的应用程序功能。它们看起来与 Java 的 Swing 和 JavaFX 非常不同。
private void Mouse_Move(object sender, MouseEventArgs e)
{
var element = (UIElement) e.Source;
var c = Grid.GetColumn(element);
var r = Grid.GetRow(element);
if (c == 0 && r == 0)
{
MenuButton.Fill = Brushes.Aqua;
}
else
{
MenuButton.Fill = Brushes.Black;
}
}
<Grid ShowGridLines="False" Background="#282828">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="68"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Column="0" Grid.RowSpan="1000">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#1c1c1c" Offset="0"/>
<GradientStop Color="#1c1c1c" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="MenuButton" MouseMove="Mouse_Move" Grid.Column="0" Grid.Row="0">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#1c1c1c" Offset="0"/>
<GradientStop Color="#1c1c1c" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ToggleButton Click="Button_Click" Grid.Row="0" Grid.Column="0" Height="32" Width="32" Checked = "HandleCheck" Unchecked = "HandleUnchecked">
<ToggleButton.Template>
<ControlTemplate>
<Image Width="32" Height="32" Source="Resources/menu.png"></Image>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
</Grid>
使用MouseLeave
和MouseEnter
代替MouseMove
XAML
<Rectangle x:Name="MenuButton" MouseEnter="Mouse_Enter" Grid.Column="0" Grid.Row="0" MouseLeave="Mouse_Leave">
C#
private void Mouse_Enter(object sender, MouseEventArgs e)
{
var element = (UIElement) e.Source;
var c = Grid.GetColumn(element);
var r = Grid.GetRow(element);
if (c == 0 && r == 0)
{
MenuButton.Fill = Brushes.Aqua;
//Change fill to Aqua when the cursor enters
}
}
private void Mouse_Leave(object sender, MouseEventArgs e)
{
var element = (UIElement) e.Source;
var c = Grid.GetColumn(element);
var r = Grid.GetRow(element);
if (c == 0 && r == 0)
{
MenuButton.Fill = Brushes.Black;
//Change fill to Black when the cursor leaves
}
}
当我将鼠标悬停在按钮上时,它会变成浅绿色,但是一旦移开光标它就不会切换回黑色。
我是 C# 的新手,正在尝试掌握基本的应用程序功能。它们看起来与 Java 的 Swing 和 JavaFX 非常不同。
private void Mouse_Move(object sender, MouseEventArgs e)
{
var element = (UIElement) e.Source;
var c = Grid.GetColumn(element);
var r = Grid.GetRow(element);
if (c == 0 && r == 0)
{
MenuButton.Fill = Brushes.Aqua;
}
else
{
MenuButton.Fill = Brushes.Black;
}
}
<Grid ShowGridLines="False" Background="#282828">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="68"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Column="0" Grid.RowSpan="1000">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#1c1c1c" Offset="0"/>
<GradientStop Color="#1c1c1c" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="MenuButton" MouseMove="Mouse_Move" Grid.Column="0" Grid.Row="0">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#1c1c1c" Offset="0"/>
<GradientStop Color="#1c1c1c" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ToggleButton Click="Button_Click" Grid.Row="0" Grid.Column="0" Height="32" Width="32" Checked = "HandleCheck" Unchecked = "HandleUnchecked">
<ToggleButton.Template>
<ControlTemplate>
<Image Width="32" Height="32" Source="Resources/menu.png"></Image>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
</Grid>
使用MouseLeave
和MouseEnter
代替MouseMove
XAML
<Rectangle x:Name="MenuButton" MouseEnter="Mouse_Enter" Grid.Column="0" Grid.Row="0" MouseLeave="Mouse_Leave">
C#
private void Mouse_Enter(object sender, MouseEventArgs e)
{
var element = (UIElement) e.Source;
var c = Grid.GetColumn(element);
var r = Grid.GetRow(element);
if (c == 0 && r == 0)
{
MenuButton.Fill = Brushes.Aqua;
//Change fill to Aqua when the cursor enters
}
}
private void Mouse_Leave(object sender, MouseEventArgs e)
{
var element = (UIElement) e.Source;
var c = Grid.GetColumn(element);
var r = Grid.GetRow(element);
if (c == 0 && r == 0)
{
MenuButton.Fill = Brushes.Black;
//Change fill to Black when the cursor leaves
}
}