How can I from a ViewModel
, 开发者_C百科find out what is the selected text? What I am doing wanting to accomplish is similar to the WMD Markdown Editor here on StackOverflow when you bold/format text. Work on the selected text and modify it
Doesn't seem right for you to manage the selection in the ViewModel, that should not contain functionality, only business data. But in any case:
Bind the SelectionChanged event for the textbox:
<TextBox SelectionChanged="TextBox_SelectionChanged"/>
And then handle the selection in whatever way you find pleasant:
private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
string thisIsTheSelectedText = ((TextBox) sender).SelectedText;
}
Or bind it twoway to a property on the ViewModel:
<TextBox SelectedText="{Binding MyModelProperty, Mode=TwoWay}"/>
精彩评论