C# 无法将单击事件传递/绑定到WPF用户控件

C# 无法将单击事件传递/绑定到WPF用户控件,c#,wpf,events,icommand,C#,Wpf,Events,Icommand,我试图将单击事件传递给WPF用户控件中的按钮 我的用户控件的xaml部分: <UserControl> <Grid> <Button Name="btnlrg" Command="{Binding Command, RelativeSource={RelativeSource AncestorType={x:Type local:ButtonLarge}}}" Click="{Binding

我试图将单击事件传递给WPF用户控件中的按钮

我的用户控件的xaml部分:

 <UserControl>
    <Grid>
        <Button Name="btnlrg"
            Command="{Binding Command, RelativeSource={RelativeSource AncestorType={x:Type local:ButtonLarge}}}"
            Click="{Binding Click, RelativeSource={RelativeSource AncestorType={x:Type local:ButtonLarge}}}">
            <Button.Content>
                <StackPanel Orientation="Horizontal">
                    <!-- shortened -->
                </StackPanel>
            </Button.Content>
        </Button>
    </Grid>
</UserControl>
public partial class ButtonLarge : UserControl
{
    public ButtonLarge()
    {
        InitializeComponent();

    }

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty TextProperty =
      DependencyProperty.Register("Text", typeof(string), typeof(ButtonLarge), new UIPropertyMetadata(""));

    public ImageSource Image
    {
        get { return (ImageSource)GetValue(ImageProperty); }
        set { SetValue(ImageProperty, value); }
    }

    public static readonly DependencyProperty ImageProperty =
       DependencyProperty.Register("Image", typeof(ImageSource), typeof(ButtonLarge), new UIPropertyMetadata(null));


    //Make Commands available in UserControl
    public static readonly DependencyProperty CommandProperty = 
        DependencyProperty.Register("Command", typeof(ICommand), typeof(ButtonLarge));

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    //Make Click Event available in UserControl
    public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ButtonLarge));

     public event RoutedEventHandler Click
    {
        add { btnlrg.AddHandler(ButtonLarge.ClickEvent, value); }
        remove { btnlrg.RemoveHandler(ButtonLarge.ClickEvent, value); }
    }

}
用户控件的使用:

<ui:ButtonLarge Image="{StaticResource Icon}" Text="Ok" Click="newClickEvent"/>


我不能在这里继续:(有人能帮我吗?

您可以简单地让一个成员变量访问该按钮:

<Button x:Name="button" .../>

或者像这样:

public static readonly RoutedEvent ClickEvent =
    ButtonBase.ClickEvent.AddOwner(typeof(ButtonLarge));

public event RoutedEventHandler Click
{
    add { button.AddHandler(ClickEvent, value); }
    remove { button.RemoveHandler(ClickEvent, value); }
}

ButtonLarge不应该从Button继承吗?thx为了您的回答,我更新了我的源代码…我可以像在我的命令中那样使用绑定吗?不,您不能。也没有必要这样做,因为命令应该足够了。好的thx,我已经根据您的示例更新了我的源代码,但它无法工作删除ButtonLarge.ClickEvent和我们e ButtonBase.ClickEvent.Or write
public static readonly RoutedEvent ClickEvent=ButtonBase.ClickEvent.AddOwner(typeof(ButtonLarge));
public static readonly RoutedEvent ClickEvent =
    ButtonBase.ClickEvent.AddOwner(typeof(ButtonLarge));

public event RoutedEventHandler Click
{
    add { button.AddHandler(ClickEvent, value); }
    remove { button.RemoveHandler(ClickEvent, value); }
}