Maybe the obvious answer is that I need to use a UserControl but I'd like to know if this is possible.
I want to customize a ComboBox to display an additional button. I've been able to create a template that renders the button next to the built-in drop down button. Now, how can I wire the Click 开发者_StackOverflowevent or access any of its properties (for example IsEnabled).
You don't need a UserControl, but you do have to inherit from ComboBox to extend it. You would write something like this:
[TemplatePart(Name = "PART_ExtraButton", Type = typeof(Button))]
public class ExtendedComboBox: ComboBox {
    private Button extraButton = new Button();
    public Button ExtraButton { get { return extraButton; } private set { extraButton = value; } }
    public static readonly RoutedEvent ExtraButtonClickEvent = EventManager.RegisterRoutedEvent("ExtraButtonClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ExtendedComboBox));
    public event RoutedEventHandler ExtraButtonClick {
        add { AddHandler(ExtraButtonClickEvent, value); }
        remove { RemoveHandler(ExtraButtonClickEvent, value); }
    }
    void OnExtraButtonClick(object sender, RoutedEventArgs e) {
        RaiseEvent(new RoutedEventArgs(ExtraButtonClickEvent, this));
    }
    public bool IsExtraButtonEnabled {
        get { return (bool)GetValue(IsExtraButtonEnabledProperty); }
        set { SetValue(IsExtraButtonEnabledProperty, value); }
    }
    public static readonly DependencyProperty IsExtraButtonEnabledProperty =
        DependencyProperty.Register("IsExtraButtonEnabled", typeof(bool), typeof(ExtendedComboBox), new UIPropertyMetadata(OnIsExtraButtonEnabledChanged));
    private static void OnIsExtraButtonEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        ExtendedComboBox combo = (ExtendedComboBox)d;
        combo.ExtraButton.IsEnabled = (bool)e.NewValue;
    }
    public override void OnApplyTemplate() {
        base.OnApplyTemplate();
        var templateButton = Template.FindName("PART_ExtraButton", this) as Button;
        if(templateButton != null) {
            extraButton.Click -= OnExtraButtonClick;
            extraButton = templateButton;
            extraButton.Click += new RoutedEventHandler(OnExtraButtonClick);
            extraButton.IsEnabled = this.IsExtraButtonEnabled;
        }
    }
}
It's sometimes possible using attached properties/events
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论