使用自定义ControlTemplate按钮单击绑定?

时间:2013-01-01 21:48:41

标签: c# winrt-xaml

我正在尝试创建一个设置弹出窗口(C#中的Windows 8 App),并且由于弹出窗口的背景是白色的,我喜欢有点'反转页面控件以使它们在默认情况下可见。我设法改变了TextBoxes的边框颜色。但是,当我“反转”所有按钮内容并单击该按钮时,应用程序会在此行中断开。

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

这是usercontrol的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)
    {

    }
}

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

做什么/不做什么?

1 个答案:

答案 0 :(得分:1)

你错过了前景中的“e”

<ColorAnimation To="White" Duration="0" Storyboard.TargetProperty="(ContentPresenter.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="cpresent"/>