I have recently started moving from WinForms to WPF and I have been trying to learn MVVM practices. At the moment I am converting an old project for that helps me monitor server activity.
I have multiple views bound to a content control on my mainwindow that are toggled through with radio buttons. In one of these views (LogView), I have a custom richtextbox that formats, colors, and appends text via an AppendText method.
I need to be able to call append method when an event fires on my mainviewmodel. My initial thought was to create a dependency property in the rtb then bind to it as just call the method in the property declaration.
I don't know if I am going about this the right way or not I am still new at this. No text is being displayed and I am not getting any errors and I know the connection is being made.
Most of the examples/videos I find on the topic only talk about binding properties or lists and usually fire based on some input from the user rather than some automated input.
Thanks for any help!
I have multiple views bound to a content control on my mainwindow that are toggled through with radio buttons. In one of these views (LogView), I have a custom richtextbox that formats, colors, and appends text via an AppendText method.
I need to be able to call append method when an event fires on my mainviewmodel. My initial thought was to create a dependency property in the rtb then bind to it as just call the method in the property declaration.
ExRichTextBox on LogView:
public partial class ExRichTextBox : RichTextBox
{
public static readonly DependencyProperty AppendMessageProperty =
DependencyProperty.Register("AppendMessage", typeof(string),
typeof(ExRichTextBox), new PropertyMetadata(string.Empty));
public string AppendMessage
{
get { return (string)GetValue(AppendMessageProperty); }
set {
SetValue(AppendMessageProperty, value);
AppendText(value);
}
}
LogView Xaml:
<controls:ExRichTextBox x:Name="LogWindow"
Margin="0,0,0,5"
IsReadOnly="True"
Padding="1,1,1,1"
Background="Transparent"
BorderThickness="0"
VerticalScrollBarVisibility="Auto"
AppendMessage="{Binding LastMessage, Mode=OneWay}" />
MainViewModel:
private string _lastmsg = string.Empty;
public string LastMessage
{
get { return _lastmsg; }
set
{
_lastmsg = value;
OnPropertyChanged();
}
}
// Generic event
private void OnDataArrival(string data)
{
LastMessage = data;
}
I don't know if I am going about this the right way or not I am still new at this. No text is being displayed and I am not getting any errors and I know the connection is being made.
Most of the examples/videos I find on the topic only talk about binding properties or lists and usually fire based on some input from the user rather than some automated input.
Thanks for any help!