为生成的按钮后面的自定义代码提供事件处理程序
本文关键字:代码 自定义 事件处理 程序 按钮 | 更新日期: 2023-09-27 18:28:24
大家好,我该如何给我的按钮一个eventhanlderb2.点击+=新建RoutedEventHandler(BtnChiefAns);我试过了,但不起作用
我的按钮是自定义按钮这是称之为的代码
ButtonLeft b2=新按钮左();
当我这样做的时候b2.点击+=新建RoutedEventHandler(BtnChiefAns);它会突出显示点击词,并说未知成员"点击"自定义用户控制"
这是我定制按钮的代码
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:edc="clr-namespace:Microsoft.Expression.Controls;assembly=Microsoft.Expression.Drawing"
mc:Ignorable="d"
x:Class="Volunteer.LayoutRootControl" Height="127" Width="200">
<UserControl.Resources>
<Style x:Key="ButtonStyle8" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<edc:Callout AnchorPoint="0.85,1.19" CalloutStyle="Rectangle" Fill="#FFE054EF" FontSize="14.666999816894531" Stroke="Black"/>
<ContentPresenter Height="96" Width="196"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Button Style="{StaticResource ButtonStyle8}" HorizontalAlignment="Left" Height="102" VerticalAlignment="Top" Width="200">
<Button.Content>
<StackPanel Orientation="Horizontal" Width="197" Margin="-40,-34,-41,-32">
<TextBlock Width="196" x:Name="BtnIN3" Text="" FontSize="22" TextWrapping="Wrap" Margin="0,0,0,-12" Height="95" />
</StackPanel>
</Button.Content>
</Button>
我需要能够点击这个按钮:(提前感谢!
由于这是一个UserControl,因此它没有Click事件。你需要实施它。
在UserControl的代码中,添加以下内容:
public event RoutedEventHandler Click;
然后,为XAML中的Button挂起click事件,并在实现中编写如下内容:
if (Click != null)
{
Click(this, e); // Where "e" is the parameter you got from the button.
}
如果您的UserControl
只有一个Button
,则可以在没有UserControl
的情况下创建具有自定义样式的Button
。然后你可以在xaml中创建按钮,或者以这种方式:
Button b = new Button()
{
Style = (Style)Application.Current.Resources["ButtonStyle8"]
};
b.Click += ...
将更容易、更高效