Adding a button to a combobox in wpf
Pretty much I have an Editable Combobox and I want to add a button to the right of the drop down button which clears the selected item. So...开发者_如何转开发
|TextBox |X|v|
I was thinking something like...
<Style...>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ControlTemplate.Resources>
<Style TargetType="{x:Type TextBox}">...Add button here...</Style>
</ControlTemplate.Resources>
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I do not think that your proposed approach will work. As soon as you set the Template
property in the style, you will also have to redefine its visual representation. You could, however, define an adjusted ControlTemplate
for a TextBox
(as you suggested) in the Resources
section of the ComboBox
(not within its Template!).
<ComboBox ...>
<ComboBox.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Template">
<ControlTemplate TargetType="{x:Type TextBox}">
<!-- define template for TextBox with an additional "clear" button -->
</ControlTemplate>
</Setter>
</Style>
</ComboBox.Resources>
</ComboBox>
But then you still have the problem how to handle the click on this button...
A clean solution would be to sub-class ComboBox
and provide an ICommand
which clears the current selection. Then you would override the ComboBox
's ControlTemplate
, add a "clear" button besides the drop down button and bind that button to the new ICommand
. This definitely is quite some work, but in the end you have a clean solution which you can easily extend later on.
精彩评论