WPF:如何为多个不同的Click事件使用一个User控件

时间:2015-03-09 18:27:14

标签: c# wpf xaml events controls

我尝试为主页上的所有按钮创建用户控件,每个用户控件都应该有不同的Click事件。我试图通过向User控件(适用于标签和图像)添加属性来解决此问题,但我无法找到Click事件的任何解决方案。

ImageLabelButton.xaml:

<UserControl x:Class="SC.UI.WPF.Controls.ImageLabelButton"
<Grid>
    <Button Name="BtnClick">
        <StackPanel>
                <Image Name="ImageButton"/>
                <Label Name="LabelButton"/>
        </StackPanel>
    </Button>
</Grid>

ImageLabelButton.xaml.cs:

....
  <!-- works -->
  public string Name
  {
      get { return this.LabelButton.Content.ToString(); }
      set { this.LabelButton.Content = value; }
  }
  <!-- works -->
  public ImageSource SetSource
  {
      get { return ImageButton.Source; }
      set { ImageButton.Source = value; }
  }
  <!-- doesn't work -->
  public EventHandler ButtonAction
  {
      get { return BtnClick.Click; }
      set { BtnClick.Click= value; }
  }

Implementation.xaml:

....
<controls:ImageLabelButton Name="first" SetSource="test.png" ButtonAction="Click1"/>
<controls:ImageLabelButton Name="first" SetSource="test.png" ButtonAction="Click2"/>
<controls:ImageLabelButton Name="first" SetSource="test.png" ButtonAction="Click3"/>

2 个答案:

答案 0 :(得分:0)

您的用户控件上需要一个可以从Implementation.xaml

访问的RoutedEvent

ImageLabelButton.xaml

<Button Name="BtnClick" Click="Submit_Click">
    <StackPanel>
            <Image Name="ImageButton"/>
            <Label Name="LabelButton"/>
    </StackPanel>
</Button>

ImageLabelButton.cs

private void Submit_Click(object sender, RoutedEventArgs e)
    {
        RaiseEvent(new RoutedEventArgs(ClickEvent, this));
    }

    public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent(
        "Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyUserControl));

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

Implementation.xaml

<controls:ImageLabelButton Name="first" SetSource="test.png" Click="Click1"/>

Implementation.xaml.cs

private void Click1(object sender, RoutedEventArgs e)
{

}

答案 1 :(得分:0)

您只需将事件处理程序设置为用户控件的属性,并将其设置在使用代码中:

用户控制XAML:

<Grid>
    <Button Name="BtnClick" Click="BtnClick_DoClick">
        <StackPanel>
            <Image Name="ImageButton"/>
            <Label Name="LabelButton"/>
        </StackPanel>
    </Button>
</Grid>

用户控制代码隐藏:

    public EventHandler<RoutedEventArgs> ButtonAction;

    private void BtnClick_DoClick(object sender, RoutedEventArgs e)
    {
        if (ButtonAction != null) ButtonAction(sender, e);
    }

实施XAML:

<controls:ImageLabelButton Name="first1" SetSource="test.png" />
<controls:ImageLabelButton Name="first2" SetSource="test.png" />
<controls:ImageLabelButton Name="first3" SetSource="test.png" />

背后的实施代码:

    public MainWindow()
    {
        InitializeComponent();

        first1.ButtonAction = Button1;
        first2.ButtonAction = Button2;
        first3.ButtonAction = Button3;
    }

    private void Button3(object sender, RoutedEventArgs e) { MessageBox.Show("Pressed 3"); }
    private void Button2(object sender, RoutedEventArgs e) { MessageBox.Show("Pressed 2"); }
    private void Button1(object sender, RoutedEventArgs e) { MessageBox.Show("Pressed 1"); }

虽然您无法以这种方式在XAML中设置事件处理。