How can i set the Text
of a Textbox
using wpf and Devexpress free MVVM when a button is clicked?
This is the xaml code of my Textbox
<TextBox Width="200" Grid.Column="1" Text="{Binding SelectedText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="14"/>
this is my VM code
public virtual string SelectedText { get; set; }
void AutoUpdateCommandExecute()
{
Console.WriteLine("Code is Executing");
SelectedText = "TextBox Text is Not Changing";
}
The code is Executing but it doesn't change the Text
of the Textbox
But when i type something in the textbox
and get the text of that textbox using this code.
void AutoUpdateCommandExecute()
{
Console.WriteLine("Code is Executing");
Console.WriteLine(SelectedText);
}
It prints the Text i typed in the Textbox. So what did i do wrong? I can't set the Text?
Thank you.
your selected text needs to be either a DependencyProperty which will then notify the binding that it's value has changed...
public static readonly DependencyProperty SelectedTextProperty = DependencyProperty.Register("SelectedText", typeof(string), typeof(YourClassType));
public string SelectedText
{
get { return (string)GetValue(SelectedTextProperty ); }
set { SetValue(SelectedTextProperty , value); }
}
OR
your VM needs to implement INotifyPropertyChanged interface and in the SelectedText setter you need to fire a property changed event...
public class VM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _selectedText;
public string SelectedText
{
get { return _selectedText; }
set
{
_selectedText = value;
RaisePropertyChanged("SelectedText");
}
}
private void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}