You can do this with Command and CommandParameter
First, bind the button to an ICommand, like:
<Button Content="Go" Command="{Binding MyCommand}" CommandParameter="{Binding ElementName=myRichTextBox, Path=Selection}" />
<RichTextBox Name="myRichTextBox" />
Then in your ViewModel or Controller or Code-behind or wherever, you expose the ICommand as a property,and point it to a method to do the work, like...
public ICommand MyCommand
{
get
{
if (_queryCommand == null)
{
_queryCommand = new RelayCommand<TextSelection>(DoWork);
}
return _queryCommand;
}
}
private void DoWork(TextSelection param)
{
string selectedText = param.Text;
// Build your control here...
// probably put it in an ObservableCollection<Control> which is bound by an Items Control, like a ListBox
}
Note: I have used the RelayCommand from Josh Smith's excellent MVVM Foundation, but you could equally use a RoutedUICommand for example (which would add the extra benefit of letting you associate input gestures to your command)