0
votes

I am using the MVVM pattern and trying to stay with the view-viewModel 1-1 relationship. I have a window with a RichTextBox and I'm coding the ability for the user to add a hyperlink via firing a dialog window from an ' Add Hyperlink' ribbon button.

I've got the property into the Hyperlink dialog window by passing it into the constructor, this works fine but I'm having problems sending the appended value back to the parent ViewModel.

Here's the property which holds my RTB text from my main page ViewModel code:

    private viewArticle _ModelviewArticle = new viewArticle();
    public viewArticle ModelviewArticle
    {
        get { return _ModelviewArticle; }
        set
        {
            OnPropertyChanged("ModelviewArticle");
        }
     }

This property holds my text which is bound to the RichTextBox. I then show the hyperlink window via a button clicked command and append the user's chosen hyperlink & link text from textbox input in its own ViewModel (this is still not quite finished):

 public static string BuildRtfHyperlink(string Url, string Linktext)
    {
        if (Url != null && Linktext != null)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat(@"{0}{{\ul\\cf3\ltrch {{\field{{\*\fldinst {{ HYPERLINK ""{1}""}}}}{{\fldrslt {{{2}}}}}}}}}\li0\\ri0\sa0\sb0\fi0\ql\par}}", _ArticleBody, Url, Linktext);

            return sb.ToString();               
        }
        else
        {
            MessageBox.Show("Please enter valid linked text and URL");
            return ""; 
        }              
    }

Is there any way to append the returned string to my _ModelviewArticle property in the other ViewModel or am I best using the one ViewModel for both the main page and hyperlink page views as they share functionality?

Thanks

1

1 Answers

0
votes

If you pop up the additional dialog from the main view (the window with the rich textbox) then you can take the returned value (if any), decorate it as necessary (i.e. add the appropriate rich text formatting) and then insert it directly into the rich textbox. The bound property ModelviewArticle should automatically be updated due to the binding.

Why make the view responsible for this?

Because it is an action on the view itself that causes the need for the popup dialog. The viewmodel is agnostic to the view, it has no idea what is on there, therefore you should not pollute it with minor view specific functionality like this.

Does this mean you have to use some code behind in the view?

Yes, you'll have to had a small amount of code to the code behind but there is nothing wrong with this. It is view related code, this is where it belongs. Depending on where you learnt WPF you may have seen mentioned that you shouldn't have code behind in a view - that is nonsense. If you can achieve that then great, but in reality it is frequently necessary. Just keep it where it belongs.

Having a viewmodel for a tiny popup is probably overkill.

Yes you can have it, but the reality is that it simply adds complexity for what is a very simple popup with a very simple job. There are numerous code samples for dialog services, those will usually involve viewmodels because they are dealing with more complicated popup views which take more than a couple of bits of text. There is no reason why your Hyperlink view can't be a simple view with some code behind as it has a very limited task to perform.