Hi,
I've checked out this post but couldn't find a solution to my problem. I'm trying to change XamComboEditor in order to show an additional button next to dropdown button which clears the selected combo value. I've managed to show the extra button by using style but couldn't find a way to clear the value when the button is clicked.
Either by using style or inheriting from XamComboEditor, how can I do this?
I've attached the sample project that includes my failed attempts. I hope someone can help on this very basic problem.
Thanks in advance
No answer?
Hello,
I apologize that your post was not answered sooner. I took a look at your sample and everything is perfect except one thing - the clear button cannot be found in the ApplyTemplate method override. You have placed the button in the dropdown toggle button of the XamComboEditor but you are overriding the OnApplyTemplate of the XamComboEditor. The reason why the clear button cannot be found is because the template of the toggle button is not created yet. What you need to do is call the ApplyTemplate method before trying to get the clear button :
var dropDownButton = this.Template.FindName(ElementDropDownButton, this) as ToggleButton;
dropDownButton.ApplyTemplate();
This way, the template will be applied and the button will be found correctly.
Thanks for the relpy!
I've added the statement that you've suggested and it worked but since I don't' know anything about inheriting controls and overriding OnApplyTemplate method I've two more questions:
1. I thought that OnApplyTemplate is called only once but apparently it is not. So thinking that it was called once I've attached the event handler only when my private variable clearButton was null in order to skip reassignment of event handler. The thing is I saw that when I skip further assignments the previous event assignment doesn't work anymore. I don't understand why.
So I've changed my override as below in order to remove the previous event handler on clear button and also attach a new one. Is the below code OK?
public override void OnApplyTemplate() { base.OnApplyTemplate(); var dropDownButton = this.Template.FindName(ElementDropDownButton, this) as ToggleButton; if (dropDownButton != null) { dropDownButton.ApplyTemplate(); if (clearButton != null) clearButton.Click -= new RoutedEventHandler(ClearValue); clearButton = dropDownButton.Template.FindName(ElementClearButton, dropDownButton) as Button; if (clearButton != null) clearButton.Click += new RoutedEventHandler(ClearValue); } }
2. When I clear the value by clicking the clear button and then try to select a new value from the dropdown, BEFORE I focus out ComboBox, the clear button doesn't work anymore. It's related with the case I mentioned above. When I click the dropdown button, the OnApplyTemplate is called again but this time dropdown button is not found.
Being have to focus out the combo in order to use the clear button is not easy. So how can I fix this problem?
Thanks so much. It works perfect!
This is because the XamComboEditor is using two different templates when displaying its content - EditTemplate and Template. The Edit template is used when the editor is in edit mode. If you look inside the EditTemplate you will see that there is no toggle button - the dropdown is part of the Template but not in the edit template. Therefore, I would recommend taking the clear button out of the DropDownButtonTemplate and putting it in both the Template and EditTemplate ControlTemplates.
This way you can simplify your OnApplyTemplate code :
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var clearButton = this.Template.FindName(ElementClearButton, this) as Button;
if (clearButton != null)
clearButton.Click -= ClearValue;
clearButton.Click += new RoutedEventHandler(ClearValue);
}
I have attached your project modified.