如何在WPF用户控件中触发单击事件(带按钮的文本框)?

时间:2013-10-31 12:23:04

标签: c# wpf xaml user-controls

我为WPF创建了一个用户控件,如Windows 8密码框模型。

我的UserControl XAML代码 -

 <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>

enter image description here

编辑1:

我已在我的项目中包含此Usercontrol。但是在Wpf应用程序中实现UserControl时我的UserControl没有Click事件。所以我将Click =“UserControl1_Click”添加到Xaml。但它通过错误

  

XML命名空间中不存在“Click”属性   'CLR-名称空间:NumericTextbox;装配= NumericTextbox'。

我的应用程序Xaml代码 -

<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>

6 个答案:

答案 0 :(得分:3)

您可以在UserControl上具有绑定到按钮的Click事件的依赖项属性。

编辑:

在您的UserControl中,您可以拥有

    public EventHandler MyProperty
    {
        get { return (EventHandler)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }
    public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(EventHandler), typeof(ownerclass));

然后在XAML中你有:

 <button Click="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MyProperty}" />

虽然,我认为最好使用ICommand而不是事件处理程序,但原理是相同的。

答案 1 :(得分:1)

可能会帮助你

  <Button Style="{StaticResource ButtonWithoutHover}" Height="25"
   Width="20" BorderThickness="3" BorderBrush="White"
   DockPanel.Dock="Right" Background="CadetBlue" Click="Button_Click">
                               <Button.Content>
                                   <Label Content="->" Foreground="White" />
                               </Button.Content>

</Button>

答案 2 :(得分:1)

您可以创建一个命令,使命令绑定如下:

创建RoutedUiCommand命令:

Class Command
{
     Static RoutedUICommand ButtonClick = New RoutedUICommand("Buttonclick", "ButtonClick", TypeOf(Window))
}

然后在xaml

<Window.CommandBindings>
    <CommandBinding Command="local:Command.ButtonClick" Executed="CommandBinding_ButtonClick" />
</Window.CommandBindings>

在方法CommandBinding_ButtonClick内你制作逻辑。

最后将命令添加到按钮:

<Button Style="{StaticResource ButtonWithoutHover}" Height="25" Width="20" BorderThickness="3"    BorderBrush="White" DockPanel.Dock="Right" Background="CadetBlue" Command=local:Command.ButtonClick>
                <Button.Content>
                    <Label Content="->" Foreground="White" />
                </Button.Content>
            </Button>

答案 3 :(得分:0)

当您设计用户控件时,您是否在按钮上尝试了此操作? (记得选择那个编辑模板的东西)

https://i-msdn.sec.s-msft.com/dynimg/IC616903.png

P.S。当我设计用户控件时,我总是喜欢使用混合。

答案 4 :(得分:-1)

您始终可以为按钮添加命令绑定。 这样,您可以将按钮链接到方法并控制何时可以使用按钮。

雷切尔就此问题提供了很好的解释here

答案 5 :(得分:-3)

我们在添加Click事件后创建了UserControl1.then,UserControl1.Desginer.cs文件将如下所示。

  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文件中将具有Button click事件的主体

private void button1_Click(object sender, EventArgs e)
        {

        }

这可能会对你有所帮助.Plz让我知道

相关问题