C# 如何在 WPF 用户控件(带按钮的文本框)中触发单击事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19706459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Trigger Click Event in WPF User control (Textbox with Button)?
提问by Sagotharan
I have Created one User control for WPF Like Windows 8 Password box Model.
我为 WPF 创建了一个用户控件,如 Windows 8 密码框模型。
My UserControl XAML Code -
我的 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>
Edit 1:
编辑1:
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
我在我的项目中包含了这个用户控件。但是在 Wpf 应用程序中实现 UserControl 时,我的 UserControl 没有单击事件。所以我将 Click="UserControl1_Click" 添加到 Xaml。但它通过一个错误
The property 'Click' does not exist in XML namespace 'clr-namespace:NumericTextbox;assembly=NumericTextbox'.
XML 命名空间“clr-namespace:NumericTextbox;assembly=NumericTextbox”中不存在属性“Click”。
My Application Xaml Code-
我的应用程序 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>
采纳答案by Debashrita
As we have created the UserControl1.then after adding the Click event the UserControl1.Desginer.cs file will look like this.
由于我们已经创建了 UserControl1.then,在添加 Click 事件后,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);
}
and in the UserControl1.cs file will have the body of the Button click event
并且在 UserControl1.cs 文件中会有按钮点击事件的主体
private void button1_Click(object sender, EventArgs e)
{
}
this may help you.Plz let me know
这可能对你有帮助。请告诉我
回答by David Pilkington
You could have a Dependency Property on the UserControl that you bind to the Click event of the button.
您可以在绑定到按钮的 Click 事件的 UserControl 上有一个依赖属性。
EDIT:
编辑:
In your UserControl you can have
在您的 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));
then in the XAML you have:
然后在 XAML 中你有:
<button Click="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MyProperty}" />
Although, I think that it is best practice to use ICommand instead of the event handler but the principle is the same.
虽然,我认为最好使用 ICommand 而不是事件处理程序,但原理是相同的。
回答by Debashrita
may be this will help you
可能这会帮助你
<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>
回答by Alexandre
You can create a command an make a command binding like this:
您可以像这样创建命令并进行命令绑定:
Create RoutedUiCommand command:
创建 RoutedUiCommand 命令:
Class Command
{
Static RoutedUICommand ButtonClick = New RoutedUICommand("Buttonclick", "ButtonClick", TypeOf(Window))
}
Then in xaml
然后在xaml
<Window.CommandBindings>
<CommandBinding Command="local:Command.ButtonClick" Executed="CommandBinding_ButtonClick" />
</Window.CommandBindings>
inside the method CommandBinding_ButtonClick
you make the logic.
在CommandBinding_ButtonClick
你制作逻辑的方法中。
and finally add the command to the button:
最后将命令添加到按钮:
<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>
回答by Dark Templar
When you design your user control have you tried this on your button? (remember to choose that edit template thing)
当你设计你的用户控件时,你有没有在你的按钮上试过这个?(记得选择那个编辑模板的东西)
https://i-msdn.sec.s-msft.com/dynimg/IC616903.png
https://i-msdn.sec.s-msft.com/dynimg/IC616903.png
p.s. when I design user controls I always prefer to use blend.
ps 当我设计用户控件时,我总是更喜欢使用混合。