아마도 명백한 대답은 UserControl을 사용해야한다는 것입니다.하지만 이것이 가능한지 알고 싶습니다.WPF 템플릿 컨트롤에서 새 이벤트/속성을 노출 할 수 있습니까?
추가 버튼을 표시하도록 ComboBox를 사용자 정의하고 싶습니다. 기본 제공 드롭 다운 단추 옆에 단추를 렌더링하는 템플릿을 만들 수있었습니다. 이제 어떻게 Click 이벤트를 연결하거나 속성 (예 : IsEnabled)에 액세스 할 수 있습니까?
아마도 명백한 대답은 UserControl을 사용해야한다는 것입니다.하지만 이것이 가능한지 알고 싶습니다.WPF 템플릿 컨트롤에서 새 이벤트/속성을 노출 할 수 있습니까?
추가 버튼을 표시하도록 ComboBox를 사용자 정의하고 싶습니다. 기본 제공 드롭 다운 단추 옆에 단추를 렌더링하는 템플릿을 만들 수있었습니다. 이제 어떻게 Click 이벤트를 연결하거나 속성 (예 : IsEnabled)에 액세스 할 수 있습니까?
UserControl은 필요하지 않지만이를 확장하려면 ComboBox에서 상속해야합니다.
그것은 연결된 속성을 사용하여 때로는 가능[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;
}
}
}
/이벤트
완벽한 대답 덕분에 많은 : 당신은 다음처럼 작성할 수 있습니다. 마침내 그 개념을 이해할 수있을 것 같습니다. – JAG