开发者

C#/WPF: How can I from a ViewModel find out what is Selected in a TextBox?

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}"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜