I am new to WPF and MVVM. I have a TextBox with some lines of text. I want to bind a piece of text highlighted by a user, to my ViewModel. How can I do this without coding in code behind?
Asked
Active
Viewed 490 times
-2
-
Welcome to SO, could you please include what have you tried so far to your question? – SamTh3D3v Nov 19 '19 at 11:03
-
please make a google search and try something befor posting, stack overflow used to be good... – Erwin Draconis Nov 19 '19 at 12:07
-
1Does this help? https://stackoverflow.com/q/32516738/1136211 – Clemens Nov 19 '19 at 12:39
-
@ErwinDraconis I already searched on google and I did not find the way to do this with mvvm. – Sara Sheikhi Nov 19 '19 at 13:22
-
@SamTheDev in fact, I open a text file in a textbox and after that I select a word from text. I have to return this selected word to a method in my viewmodel. I work with MVVM architecture and I have nothing in the Xaml code behind. I found the answers that use the code behind to do this binding but in my case, I should to respect completely to MVVM architecture. – Sara Sheikhi Nov 19 '19 at 13:30
-
1Well, you have to try something first (maybe following the link that @Clemens suggested), StackOverflow isn't a code on-demand platform ^^ – SamTh3D3v Nov 19 '19 at 13:34
-
@SamTheDev thanks for your comments but they are not very useful. – Sara Sheikhi Nov 19 '19 at 13:38
-
1@SaraSheikhi here is how to do it : https://stackoverflow.com/questions/2245928/mvvm-and-the-textboxs-selectedtext-property – Erwin Draconis Nov 19 '19 at 13:49
-
@ErwinDraconis Yes, it is probably the solution that I haven't tried yest. thank you – Sara Sheikhi Nov 19 '19 at 18:23
-
@Clemens, I haven't heard about DependencyProperty! that solved my peorblem.Thanks a lot. – Sara Sheikhi Nov 20 '19 at 09:41
-
@Clemens I have a problem, I did exactly Thomas Levesque's solution but It doesn't work, I put breakpoints for all of functions in TextBoxHelper but none of them is called when I select a text. I would like to know if there is something more to do to make it work? – Sara Sheikhi Nov 21 '19 at 10:20
1 Answers
-4
You cannot directly bind your selected text to a property. You have to implement a DependencyProperty and attach that but this is not trivial for beginners. A good explanation is here: MVVM and the TextBox's SelectedText property

Mihaeru
- 533
- 2
- 6
-
3The `Binding.Target` must be a `DependencyProperty`. The `Binding.Source` can be any public property with a getter where `BindingMode.TwoWay` and `BindingMode.OneWayToSource` requires a setter too. To make `BindingMode.TwoWay` work properly the class of the binding source must implement `INotifyPropertyChanged` and raise the `PropertyChanged` event with the property's name as event argument when the value of the property has changed. – BionicCode Nov 19 '19 at 11:28
-
1I know the requirements for binding very well and you described the basics, which do not apply here because you cannot bind the `SelectedText` property of the `TextBox` to a property because it is not a `DependencyProperty`. This is why you have to attach your own `DependencyProperty` to get access to the `TextBox::SelectedText` property. Besides that, the solution I linked to does not extend the `TextBox` in the ViewModel. Maybe you mean the solution which is linked in the comments to the question? Edit: The comment I'm answering to seems to be deleted... – Mihaeru Nov 19 '19 at 13:45
-
@Mihaeru, Thanks for your answer, I did exactly what Thomas Levesque said in your link but It doesn't work, I put breakpoints for all of functions in TextBoxHelper but none of them is called when I select a text. I would like to know if there is something more to do to make it work? – Sara Sheikhi Nov 21 '19 at 09:38
-
Have you set your SelectedText property to an initial value? If it is null, the behavior will be detached in SelectedTextChanged. – Mihaeru Nov 21 '19 at 10:44
-
@Sara Sheikhi to be more specific, the bound property in your ViewModel should be initialized and implemented like following: `private string _selectedText = string.Empty; public string SelectedText { get { return _selectedText; } set { _selectedText = value; OnPropertyChanged(() => SelectedText); } }` – Mihaeru Nov 21 '19 at 11:18
-
1