1

我正在尝试创建一个设置浮出控件(C# 中的 Windows 8 应用程序),并且由于浮出控件的背景是白色的,因此我有点想反转页面控件以使它们在默认表单中可见。我设法改变了文本框的边框颜色。但是,当我“反转”所有按钮内容并单击按钮时,应用程序会在此行中断。

if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();

这是用户控件的 XAML:

<UserControl
x:Class="tapp.Settings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:tapp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<UserControl.Resources>
    <Style x:Key="tb" TargetType="TextBox">
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="Width" Value="200"/>
    </Style>
    <Style x:Key="pb" TargetType="PasswordBox">
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="Width" Value="200"/>
    </Style>
    <Style x:Key="btn" TargetType="Button">
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="Background" Value="White"/>
        <Setter Property="ClickMode" Value="Press"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Height="{TemplateBinding Height}" Width="{TemplateBinding Width}">
                        <Rectangle x:Name="outRect"
                                   HorizontalAlignment="Stretch"
                                   VerticalAlignment="Stretch"
                                   Fill="Black">
                        </Rectangle>
                        <Rectangle x:Name="innerRect"
                                   HorizontalAlignment="Stretch"
                                   VerticalAlignment="Stretch"
                                   Margin="2"
                                   Opacity="100"
                                   Fill="White">
                        </Rectangle>
                        <ContentPresenter x:Name="cpresent" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="PointerOver">
                                    <Storyboard Duration="0">
                                        <ColorAnimation To="LightGray" Duration="0"                    Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="innerRect" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ColorAnimation To="Black" Duration="0"                    Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="innerRect" />
                                        <ColorAnimation To="White" Duration="0" Storyboard.TargetProperty="(ContentPresenter.Forground).(SolidColorBrush.Color)" Storyboard.TargetName="cpresent"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<Grid>
    <TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="Username" VerticalAlignment="Top"/>
    <TextBox x:Name="username" HorizontalAlignment="Left" Margin="10,28,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="30" Style="{StaticResource ResourceKey=tb}"/>
    <TextBlock HorizontalAlignment="Left" Margin="10,65,0,0" TextWrapping="Wrap" Text="Password" VerticalAlignment="Top"/>
    <PasswordBox x:Name="password" HorizontalAlignment="Left" Margin="10,83,0,0" VerticalAlignment="Top" Style="{StaticResource ResourceKey=pb}"/>
    <Button x:Name="login" Content="Login" HorizontalAlignment="Left" Margin="10,120,0,0" VerticalAlignment="Top" Width="200" Height="32" Style="{StaticResource btn}"/>

</Grid>

在 CS 文件中,我添加了一个处理程序,但它根本没有响应。

public sealed partial class Settings : UserControl
{
    public Settings()
    {
        this.InitializeComponent();
        login.Click+=login_Click;
    }

    private void login_Click(object sender, RoutedEventArgs e)
    {

    }
}

如果我删除按钮样式一切正常

做什么/不做什么?

4

1 回答 1

1

你错过了前景中的“e”

<ColorAnimation To="White" Duration="0" Storyboard.TargetProperty="(ContentPresenter.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="cpresent"/>
于 2013-02-05T19:55:16.560 回答