xceedsoftware / wpftoolkit

All the controls missing in WPF. Over 1 million downloads.
Other
3.87k stars 871 forks source link

When using Richtextbox and mvvm the string in null #1382

Open JonathanMiz opened 5 years ago

JonathanMiz commented 5 years ago

I'm trying to use richtextbox and binds it to a string. Everything is fine at the beginning.

This is my code.

               <toolkit:RichTextBox 
                    Text="{Binding NoteContent,
                              Source={StaticResource vm}}"
                    Name="contentRichTextBox" Grid.ColumnSpan="2"
                    SelectionChanged="contentRichTextBox_SelectionChanged"
                    Visibility="Visible">

               </toolkit:RichTextBox>

When ever I'm trying to call to a command from the view model the NoteContent is turning to null. I did a check with a regular textblock and the string is fine.

Someone has a solution for that? :)

Thanks.

nonsensesoftware commented 5 years ago

Have you tried setting UpdateSourceTrigger=PropertyChanged? From your example: <toolkit:RichTextBox Text="{Binding NoteContent, Source={StaticResource vm}, UpdateSourceTrigger=PropertyChanged}" Name="contentRichTextBox" Grid.ColumnSpan="2" SelectionChanged="contentRichTextBox_SelectionChanged" Visibility="Visible"> </toolkit:RichTextBox> As an aside, you normally don't bind to your VM via a StaticResource, and something I would consider bad practice. You need to set the DataContext of your application/window/control appropriately. You might want to provide a more comprehensive example or sample project in order to better understand what you're doing.

There are several examples on how to do so, but one I found easiest is to pass the ViewModel into the Window constructor and do the following in the constructor: public MainWindow(MainWindowViewModel viewModel) { DataContext = viewModel; // Do other stuff here }