边框控制的 UWP 默认样式

UWP default style for border control

在哪里可以找到 UWP 的默认边框控件样式?

我正在尝试更改默认样式,以便 onmouseover 会更改背景颜色。

通常您可以在这里找到默认样式:

C:\Program Files (x86)\Windows Kits\DesignTime\CommonConfiguration\Neutral\UAP.0.18362.0\Generic\generic.xaml

但是我似乎找不到边框的默认样式 class。

如果想改变鼠标悬停时Border的背景色,可以使用VisualStateManager来改变。您需要在代码隐藏中调用 VisualStateManager.GoToState() 方法以响应 PointerEnter 事件并在视觉状态上激活 "PointerOver"。另外,VisualStateManager的状态一般是作用于控件(派生自Windows.UI.Xaml.Control.Controls),但Border不是派生自Control。所以最好把Border放到一个usercontrol中。例如:

MainPage.xaml:

<Grid>
    <UserControl PointerEntered="Border_PointerEntered" PointerExited="Border_PointerExited" x:Name="MyControl">
        <Border x:Name="MyBorder" Height="30" VerticalAlignment="Top" Margin="50,0" Background="Red">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal">
                        <VisualState.Setters>
                            <Setter Target="MyBorder.Background" Value="Red"></Setter>
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="PointerOver">
                        <VisualState.Setters>
                            <Setter Target="MyBorder.Background" Value="Green"></Setter>
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </Border>
    </UserControl>
</Grid>

MainPage.xaml.cs:

private void Border_PointerEntered(object sender, PointerRoutedEventArgs e)
{
    VisualStateManager.GoToState(MyControl, "PointerOver", false);
}

private void Border_PointerExited(object sender, PointerRoutedEventArgs e)
{
    VisualStateManager.GoToState(MyControl, "Normal", false);
}

更新:

您可以直接在Border中订阅PointerEntered和PointerExited事件,然后在这些事件中改变背景,无需使用userControl。

.xaml:

<Grid>
    <Border x:Name="MyBorder" Height="30" VerticalAlignment="Top" Margin="50,0" Background="Red" PointerEntered="Border_PointerEntered" PointerExited="Border_PointerExited">
    </Border>
</Grid>

.cs:

private void Border_PointerEntered(object sender, PointerRoutedEventArgs e)
{
    MyBorder.Background = new SolidColorBrush(Colors.Green);
}

private void Border_PointerExited(object sender, PointerRoutedEventArgs e)
{
    MyBorder.Background = new SolidColorBrush(Colors.Red);
}