WPF怎么调用按钮触发事件的方法

WPF怎么调用按钮触发事件的方法,第1张

可以的,只是没什么意义

this.txtDelay_KeyDown(txtDelay, new KeyEventArgs(

    System.Windows.Input.Keyboard.PrimaryDevice,

    PresentationSource.CurrentSources.OfType<PresentationSource>().First(),

    Environment.TickCount,

    Key.A))

  // 定义事件属性

  public static readonly RoutedEvent ClickRoutedEvent = EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(ClickEventHandler), typeof(ControlName))

  

  [Description("点击时发生")]

        public event ClickEventHandler Click

        {

            add { AddHandler(ClickRoutedEvent, value) }

            remove { RemoveHandler(ClickRoutedEvent, value) }

        }

//触发点击事件,这里注册你控件上的那个点击按钮事件

void button_Click(object sender, RoutedEventArgs e)

{

    

            e.RoutedEvent = ScrollChangedRoutedEvent

            e.Source = this

            this.RaiseEvent(e)

}

原因是你在IsPressed时候设置的是Button的Background,这个值是用来确定Button正常显示时的背景的。因此没有反映。你可以通过重新定义Button的Template来解决该问题。给你个例子。你看看。

保存成文件后直接在浏览器中就能看到效果。IE、FF都可以

------------

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:sys="clr-namespace:Systemassembly=mscorlib"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

mc:Ignorable="d"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">

<Page.Resources>

<Style x:Key="InformButton" TargetType="Button">

<Setter Property="Margin" Value="2"/>

<Setter Property="FontFamily" Value="Verdana"/>

<Setter Property="FontSize" Value="11px"/>

<Setter Property="FontWeight" Value="Bold"/>

<Setter Property="Background" >

<Setter.Value>

<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

<GradientStop Color="White" Offset="0" />

<GradientStop Color="Black" Offset="0.2" />

<GradientStop Color="#007F7F7F" Offset="1" />

</LinearGradientBrush>

</Setter.Value>

</Setter>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="Button">

<Border Name="border"

BorderThickness="2"

Padding="4,2"

BorderBrush="DarkGray"

CornerRadius="13"

Width="50"

Height="50"

Background="{TemplateBinding Background}">

<Grid >

<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="contentShadow">

<ContentPresenter.RenderTransform>

<TranslateTransform X="1.0" Y="1.0" />

</ContentPresenter.RenderTransform>

</ContentPresenter>

<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content"/>

</Grid>

</Border>

<ControlTemplate.Triggers>

<Trigger Property="IsMouseOver" Value="True">

<Setter TargetName="border" Property="BorderBrush" Value="#FF4788c8" />

<Setter Property="Foreground" Value="#FF4788c8" />

</Trigger>

<Trigger Property="IsPressed" Value="True">

<Setter Property="Background" >

<Setter.Value>

<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

<GradientStop Color="White" Offset="0" />

<GradientStop Color="Blue" Offset="0.2" />

<GradientStop Color="#007F7F7F" Offset="1" />

</LinearGradientBrush>

</Setter.Value>

</Setter>

<Setter TargetName="content" Property="RenderTransform" >

<Setter.Value>

<TranslateTransform Y="1.0" X="1.0" />

</Setter.Value>

</Setter>

</Trigger>

<Trigger Property="IsDefaulted" Value="True">

<Setter TargetName="border" Property="BorderBrush" Value="#FF282828" />

</Trigger>

<Trigger Property="IsFocused" Value="True">

<Setter TargetName="border" Property="BorderBrush" Value="#FF282828" />

</Trigger>

</ControlTemplate.Triggers>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

</Page.Resources>

<Grid Height="306" Width="242">

<Grid.RowDefinitions>

<RowDefinition Height="Auto" />

<RowDefinition Height="Auto" />

<RowDefinition Height="Auto" />

<RowDefinition Height="Auto" />

<RowDefinition Height="Auto" />

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="Auto" />

<ColumnDefinition Width="Auto" />

<ColumnDefinition Width="Auto" />

<ColumnDefinition Width="Auto" />

<ColumnDefinition Width="Auto" />

</Grid.ColumnDefinitions>

<Border BorderBrush="Silver" BorderThickness="1" Grid.ColumnSpan="5" Grid.RowSpan="4" Height="240" HorizontalAlignment="Left" VerticalAlignment="Top" Width="240" />

<Button Style="{StaticResource InformButton}" Width="50" Height="50" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5,5,3,3">

<Path Fill="Green" Data="M 3,3 l 9,9 l -9,9 Z" />

</Button>

<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Column="1">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Column="3">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Column="4">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="1">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button Grid.Column="1" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="1">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button Grid.Column="3" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="1">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button Grid.Column="4" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="1">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="2">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button Grid.Column="1" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="2">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button Grid.Column="3" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="2">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button Grid.Column="4" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="2">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="3">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button Grid.Column="1" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="3">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button Grid.Column="3" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="3">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button Grid.Column="4" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="3">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Border BorderBrush="Silver" BorderThickness="1" Grid.ColumnSpan="5" Height="63" HorizontalAlignment="Left" VerticalAlignment="Top" Width="240" Grid.Row="4">

<Rectangle Stroke="Transparent" Fill="LightGoldenrodYellow" />

</Border>

<Button HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="4">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button Grid.Column="1" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="4">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button Grid.Column="3" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="4">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

<Button Grid.Column="4" HorizontalAlignment="Left" Margin="5,5,3,3" Style="{StaticResource InformButton}" VerticalAlignment="Top" Grid.Row="4">

<Path Data="M 3,3 l 9,9 l -9,9 Z" Fill="Green" />

</Button>

</Grid>

</Page>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8084099.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存