WPF UserControl Mouseover使更改子边框样式

时间:2018-11-20 17:19:02

标签: c# wpf

我尝试在用户控件上使用鼠标悬停样式,我可以使用以下代码更改用户控件的边框颜色:

    <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>

Print screen from VS 2015

2 个答案:

答案 0 :(得分:1)

最后我可以做到,您可以阅读有关代码注释的更多信息。

<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>

答案 1 :(得分:0)

为什么不在后面的代码中这样做?

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>