【问题标题】:Cannot pass/bind click event to WPF user control无法将单击事件传递/绑定到 WPF 用户控件
【发布时间】:2017-06-09 12:09:15
【问题描述】:

我正在尝试将点击事件传递给 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>

我的用户控件的 c# 部分:

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

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

【问题讨论】:

  • ButtonLarge 不应该从 Button 继承吗?

标签: c# wpf events icommand


【解决方案1】:

您可以简单地使 Button 可由成员变量访问:

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

然后在UserControl中声明一个Click事件,并直接将handler添加到Button中:

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

或者像这样:

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

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

【讨论】:

  • 感谢您的回复,我更新了我的源代码...我可以使用我的命令中的绑定来执行此操作吗?
  • 不,你不能。也没有必要这样做,因为 Command 就足够了。
  • ok thx,我已经根据你的例子更新了我的源代码,但是它不起作用
  • 移除 ButtonLarge.ClickEvent 并使用 ButtonBase.ClickEvent。
  • 或写public static readonly RoutedEvent ClickEvent = ButtonBase.ClickEvent.AddOwner(typeof(ButtonLarge));
猜你喜欢
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 2013-08-29
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多