如何在 WPF 用户控件(带按钮的文本框)中触发单击事件? [英] How to Trigger Click Event in WPF User control (Textbox with Button)?

查看:28
本文介绍了如何在 WPF 用户控件(带按钮的文本框)中触发单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 WPF 创建了一个类似于 Windows 8 密码框模型的用户控件.

I have Created one User control for WPF Like Windows 8 Password box Model.

我的用户控件 XAML 代码 -

My UserControl XAML Code -

 <Grid.Resources>
        <Style x:Key="ButtonWithoutHover" TargetType="Button">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Margin" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Name="border" 
                        BorderThickness="0"                                                        
                        Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="BorderBrush" Value="Black" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <Border BorderBrush="Black" BorderThickness="2" >
            <DockPanel Canvas.Right="2" Canvas.Top="2">
                <Button Style="{StaticResource ButtonWithoutHover}" Height="25" Width="20" BorderThickness="3" BorderBrush="White" DockPanel.Dock="Right" Background="CadetBlue" >
                    <Button.Content>
                        <Label Content="->" Foreground="White" />
                    </Button.Content>
                </Button>
                <TextBox Height="30" Width="180" FontSize="18" BorderThickness="0" Name="txtNumber" DockPanel.Dock="Left" >
                </TextBox>
            </DockPanel>
        </Border>

编辑 1:

我在我的项目中包含了这个用户控件.但是在 Wpf 应用程序中实现 UserControl 时,我的 UserControl 没有单击事件.所以我将 Click="UserControl1_Click" 添加到 Xaml.但它通过一个错误

I have include this Usercontrol in My project. But at the time of Implementing UserControl in Wpf Application There is No Click Event for my UserControl. So I add Click="UserControl1_Click" to Xaml. But It through an Error

XML 命名空间中不存在属性Click"'clr-namespace:NumericTextbox;assembly=NumericTextbox'.

The property 'Click' does not exist in XML namespace 'clr-namespace:NumericTextbox;assembly=NumericTextbox'.

我的应用程序 Xaml 代码-

My Application Xaml Code-

<Window x:Class="NumericAppication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:NumText="clr-namespace:NumericTextbox;assembly=NumericTextbox"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <NumText:UserControl1 Width="120" Height="30" Click="UserControl1_Click" />
    </Grid>
</Window>

推荐答案

因为我们已经创建了 UserControl1.然后在添加 Click 事件之后 UserControl1.Desginer.cs 文件将如下所示.

As we have created the UserControl1.then after adding the Click event the UserControl1.Desginer.cs file will look like this.

  private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(180, 38);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // UserControl1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.button1);
            this.Name = "UserControl1";
            this.Size = new System.Drawing.Size(286, 95);
            this.ResumeLayout(false);

        }

并且在 UserControl1.cs 文件中会有按钮点击事件的主体

and in the UserControl1.cs file will have the body of the Button click event

private void button1_Click(object sender, EventArgs e)
        {

        }

这可能对你有帮助.请告诉我

this may help you.Plz let me know

这篇关于如何在 WPF 用户控件(带按钮的文本框)中触发单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆