【问题标题】:How to Trigger Click Event in WPF User control (Textbox with Button)?如何在 WPF 用户控件(带按钮的文本框)中触发 Click 事件?
【发布时间】:2013-10-31 12:23:03
【问题描述】:

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

编辑 1:

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

XML 命名空间中不存在属性“Click” 'clr-namespace:NumericTextbox;assembly=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>

【问题讨论】:

  • downvoted,因为选择的答案与问题无关。 (我最好看看其他问题,并给出匹配的答案)

标签: c# wpf xaml user-controls


【解决方案1】:

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

编辑:

在您的 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 代替事件处理程序是最佳实践,但原理是一样的。

【讨论】:

  • 你能再解释一下吗...请
  • 当然:如果您在 UserControl 上有一个依赖属性,它会对外公开,​​因为它是公共的。然后,在 XAML 中,将按钮 Click 事件绑定到该 DP。我会用一个例子来更新我的答案
  • 感谢 BSoD_ZA。我在等你的榜样。
  • 亲爱的我遇到了一个错误。点击="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MyProperty}" 无效。 '{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=MyProperty}' 不是有效的事件处理程序方法名称。只有生成类或代码隐藏类上的实例方法才有效。
【解决方案2】:

也许这会对你有所帮助

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

【讨论】:

  • 亲爱的你确定这是正确的方式吗? Bzs 我收到错误...在类型“UserControl1”中找不到属性“Click”。
  • 嗨,如果您想为 userControl 上存在的 Button 添加点击事件。假设我已将 Usercontol1 添加到我的项目中,然后在 UserControl1.cs[Design]* 中添加了一个Button 然后你可以右键单击 Button 然后在 Button 的属性中设置点击事件。
  • 我知道如何在 Button 中添加点击事件。但是当我在应用程序中尝试时,UserControl1 没有事件。请确保您的解决方案与 WPF 用户控制相关。
  • 是的,我的解决方案与 WPF UserControl 相关。您想要为 UserControl 中存在的 Button 添加点击事件,或者想要为 UserControl 添加点击事件
  • 亲爱的,请参阅我的更新问题。很抱歉这个令人困惑的问题,现在我简要解释一下。
【解决方案3】:

您可以像这样创建命令并进行命令绑定:

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

【讨论】:

    【解决方案4】:

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

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

    附言当我设计用户控件时,我总是喜欢使用 blend。

    【讨论】:

      【解决方案5】:

      由于我们已经创建了 UserControl1。那么在添加 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);
      
              }
      

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

      private void button1_Click(object sender, EventArgs e)
              {
      
              }
      

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

      【讨论】:

      • 当问题是针对 WPF 时,接受的答案怎么可能针对 Winforms?
      猜你喜欢
      • 2020-05-09
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 2012-10-10
      • 2013-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多