You'll need to provide some code to show what you're trying to do. If an item is already selected, why would the RichEdit
control's template not already be set anyway?
When dealing with WPF it helps to get familiar with binding to view models, not just for things like ItemsSource
but also SelectedItem
. Given the scenario you describe I would use a binding for the SelectedItem
to the View Model then bind the RichEdit
control's template to the same View Model property, using a value converter if necessary. This way you don't need to mess around with click events and the like. Provided the View Model's property is a DependencyProperty
or fires a PropertyChanged
event (see INotifyPropertyChanged
) your RichEdit
control's template should automatically reflect the selection in the drop-down.
** Edit **
Based on your comments I'm assuming the behaviour you want is to set text based on a combo selection but allow the user to customise that text. However, if edited they should be able to re-select the combo value to reset the text. The trouble is that if the item is already selected then there is no event fired to hook into. The solution is that if the text contents change, this should de-select any combo selection (since the combo no longer reflects the contents of the text box.) Bindings can manage this quite nicely:
This example view uses a TextBox for simplicity:
<Window x:Class="UISample.UITemplateSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="UITemplateSample" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<ComboBox ItemsSource="{Binding Path=Templates}" SelectedItem="{Binding Path=SelectedTemplate}" DisplayMemberPath="Name"/>
<TextBox Grid.Row="1" AcceptsReturn="True" TextWrapping="Wrap" Text="{Binding Path=Text}"/>
</Grid>
The ViewModel:
class TemplateSampleViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<TextTemplate> Templates
{
get;
private set;
}
private TextTemplate _selectedTemplate;
public TextTemplate SelectedTemplate
{
get{ return _selectedTemplate; }
set
{
if ( _selectedTemplate == value )
return;
_selectedTemplate = value;
if (_selectedTemplate != null)
Text = _selectedTemplate.TemplateText;
firePropertyChanged("SelectedTemplate");
}
}
private string _text;
public string Text
{
get { return _text; }
set
{
if ( _text == value )
return;
_text = value;
firePropertyChanged( "Text" );
var matchingTemplate = Templates.FirstOrDefault( t => t.TemplateText == _text );
SelectedTemplate = matchingTemplate;
}
}
public TemplateSampleViewModel(IEnumerable<TextTemplate> templates)
{
Templates = new ObservableCollection<TextTemplate>(templates);
}
private void firePropertyChanged(string propertyName)
{
if ( PropertyChanged != null )
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
And wiring it up:
var viewModel = new TemplateSampleViewModel(new[]
{
new TextTemplate {Name = "First", TemplateText = "This is the first item"},
new TextTemplate {Name = "Second", TemplateText = "This is the second item"},
new TextTemplate {Name = "Third", TemplateText = "This is the third item"},
});
var test = new UITemplateSample {DataContext = viewModel};
test.Show();
This binds the combo box, then as items are selected, the text box is updated automatically. When the text box contents change, the template is inspected to see if it still matches and if not, the combo item is de-selected. If the entry matches a template then that template is selected automatically.