C# WPF UserControl鼠标盖更改子边框样式
我尝试在用户控件上使用鼠标悬停样式,我可以使用以下代码更改用户控件边框颜色:C# WPF UserControl鼠标盖更改子边框样式,c#,wpf,C#,Wpf,我尝试在用户控件上使用鼠标悬停样式,我可以使用以下代码更改用户控件边框颜色: <UserControl.Style> <Style> <Setter Property="Border.Background" Value="Blue"/> <Style.Triggers> <Trigger Property="Border.IsMouseOver" Value="True"
<UserControl.Style>
<Style>
<Setter Property="Border.Background" Value="Blue"/>
<Style.Triggers>
<Trigger Property="Border.IsMouseOver" Value="True">
<Setter Property="Border.Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Style>
但我需要改进我的用户控制风格,当我将鼠标移到它上时,子边框背景会发生变化。下面是代码:
<UserControl x:Class="R8500Receiver._UserControl.FormControl.DialBtn"
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"
d:DesignHeight="30" d:DesignWidth="43" MouseDown="UserControl_MouseDown">
<UserControl.Style>
<Style>
<Setter Property="Border.Background" Value="Blue"/>
<Style.Triggers>
<Trigger Property="Border.IsMouseOver" Value="True">
<Setter Property="Border.Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Style>
<Grid>
<Border x:Name="ParentBorder" BorderBrush="Black" BorderThickness="1" Margin="0" CornerRadius="4" Background="#FF1F1D1D" Style="{StaticResource here}">
</Border>
<Border BorderBrush="#FF7A7A7A" BorderThickness="1" Margin="2" CornerRadius="4">
<Border.Background>
<RadialGradientBrush>
<GradientStop Color="#FF1D1D1D" Offset="0.107"/>
<GradientStop Color="#FF322E2E" Offset="1"/>
<GradientStop Color="#FF303030" Offset="0.737"/>
</RadialGradientBrush>
</Border.Background>
</Border>
<Label x:Name="DialAlpha" Content="ABC" FontSize="9" VerticalAlignment="Bottom" HorizontalAlignment="Right" Foreground="White" Padding="0" Margin="0,0,4,3" HorizontalContentAlignment="Center" FontFamily="Source Code Pro Black"/>
<Label x:Name="DialNum" Content="1" VerticalAlignment="Top" HorizontalAlignment="Left" Foreground="White" Padding="0" HorizontalContentAlignment="Center" Margin="8,2,0,0" RenderTransformOrigin="0.33,0.208" FontSize="17"/>
</Grid>
为什么不在代码隐藏中执行此操作
private void mouseLeave(object sender, MouseEventArgs e)
{
myChild.Background = Brushes.AliceBlue;
}
private void mouseEnter(object sender, MouseEventArgs e)
{
myChild.Background = Brushes.Blue;
}
private void mouseLeave(object sender, MouseEventArgs e)
{
myChild.Background = Brushes.AliceBlue;
}
private void mouseEnter(object sender, MouseEventArgs e)
{
myChild.Background = Brushes.Blue;
}
为什么不在代码隐藏中这样做
private void mouseLeave(object sender, MouseEventArgs e)
{
myChild.Background = Brushes.AliceBlue;
}
private void mouseEnter(object sender, MouseEventArgs e)
{
myChild.Background = Brushes.Blue;
}
private void mouseLeave(object sender, MouseEventArgs e)
{
myChild.Background = Brushes.AliceBlue;
}
private void mouseEnter(object sender, MouseEventArgs e)
{
myChild.Background = Brushes.Blue;
}
在XAML中是这样的:
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="myParentBorder" Property="Background" Value="Red" />
</Trigger>
最后,我可以这样做,您可以阅读有关代码注释的更多信息
<UserControl x:Class="R8500Receiver._UserControl.FormControl.DialBtn"
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"
d:DesignHeight="30" d:DesignWidth="43" MouseDown="UserControl_MouseDown">
<UserControl.Resources>
<!--get child with x:type and set style on it with x:key-->
<Style TargetType="{x:Type Border}" x:Key="BorderMouseOver">
<Setter Property="Background" Value="#FF1F1D1D"/>
<Setter Property="BorderBrush" Value="Black"></Setter>
<Style.Triggers>
<!--We should bind parent control with below code-->
<DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" Value="True">
<Setter Property="Background" Value="White" />
<Setter Property="BorderBrush" Value="Black"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<Border x:Name="ParentBorder" BorderThickness="1" Margin="0" CornerRadius="4"
Style="{StaticResource BorderMouseOver}">
</Border>
<Border BorderBrush="#FF7A7A7A" BorderThickness="1" Margin="2" CornerRadius="4">
<Border.Background>
<RadialGradientBrush>
<GradientStop Color="#FF1D1D1D" Offset="0.107"/>
<GradientStop Color="#FF322E2E" Offset="1"/>
<GradientStop Color="#FF303030" Offset="0.737"/>
</RadialGradientBrush>
</Border.Background>
</Border>
<Label x:Name="DialAlpha" Content="ABC" FontSize="9" VerticalAlignment="Bottom" HorizontalAlignment="Right" Foreground="White" Padding="0" Margin="0,0,4,3" HorizontalContentAlignment="Center" FontFamily="Source Code Pro Black"/>
<Label x:Name="DialNum" Content="1" VerticalAlignment="Top" HorizontalAlignment="Left" Foreground="White" Padding="0" HorizontalContentAlignment="Center" Margin="8,2,0,0" RenderTransformOrigin="0.33,0.208" FontSize="17"/>
</Grid>
您能详细说明“改进”的含义吗?@SatishPai我需要使用按钮功能创建自定义按钮控件,但使用我的自定义样式谢谢,但我知道我们可以使用Xaml样式进行更改。我试着去学。