سؤال

once again im stuck with the databinding in WPF. This time its with the RichTextBox.

My program should write his actual state to a RichTextBox. For that each object has a variable where the actual operation performed on it is written in. (Should lead to a history of the process for each object)

So i have a class called "Message" where the text (one line) and the formattings of the text are stored.

Now i want to bind a List to the RichTextBox, respecting the stored formatting. A one-way binding would be enough.

But i didnt find anything about how to bind a List of lines to the RichTextBox and put in some formattings.

Heres the class im using to store the "lines":

    [Serializable]
class Message
{
    [field: NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private bool bold = false;
    public bool Bold
    {
        get
        {
            return bold;
        }
        set
        {
            bold = value;
            OnPropertyChanged("Bold");
        }
    }

    public bool italic { get; set; }
    public bool Italic
    {
        get
        {
            return italic;
        }
        set
        {
            italic = value;
            OnPropertyChanged("Italic");
        }
    }

    public bool landingInFront { get; set; }
    public bool LandingInFront
    {
        get
        {
            return landingInFront;
        }
        set
        {
            landingInFront = value;
            OnPropertyChanged("LandingInFront");
        }
    }

    public string messageLine { get; set; }
    public string MessageLine
    {
        get
        {
            return messageLine;
        }
        set
        {
            messageLine = value;
            OnPropertyChanged("MessageLine");
        }
    }

    Color textColor = Color.Black;
    public Color TextColor
    {
        get
        {
            return textColor;
        }
        set
        {
            textColor = value;
            OnPropertyChanged("TextColor");
        }
    }

}

I would be pleased for a example with my own class. Does not have to be complete. Just that i can see which direction it goes and how i put the formattings into the text / do the binding.

Greetings

SyLuS

Appendix: More detailed informations what i want to do

I have a program which should handle orders full automatically. The ui has an overview where the user can take a look at the actual orders. Cause the orders has to pass through different operations i want to see which operations are already performed, if there performes successfully, and so on.

In case a order is selected in the ui the detailed informations are shown on the screen. The idea is to take the order object and store the performing/performed state in the object itself. That would lead to an overview and an historie of the operations.

The output should look like that:

Order 1234
Step 1 - Check Informations
Done...

Step 2 - Providing the neseccary items
Item 1 - OK
Item 2 - OK
Item 3 - Missing

Step 3 - Ordering missing items
...

and so on.

I want to implement the output in the different steps. I have a List in each order object. So what i want to do is to add message items to the object while it is in progress.

The databinding should show the information in case a new line (message) is added or the user selects another order.

هل كانت مفيدة؟

المحلول

You should use a converter to transform your Message properties to formatted text.

For example

  public class MessageConverter: IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, string language)
    {
      var message = (Message) value;

      if(message.Italic)
      {
         var it = new Italic();
         it.Inlines.Add(new Run{Text = message.MessageLine});
         it.Inlines.Add(new LineBreak());

         return it;
      }
      else if(...)
      { 
         ...
      }
      return ...;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
      throw new NotImplementedException();
    }
  }

Also RichTextBox is some weird with binding. I think you have to find a better control provides formatting text.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top