Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF/MVVM: RichTexBox as datagrid cell editor, put formatting code into ViewModel?
    primarykey
    data
    text
    <p>I really wonder what I can put in the ViewModel. </p> <p>If its needed to be tested might some say...</p> <p>But when the properties like CaretPosition (to get the selected Text) are no dependency </p> <p>properties, then better forget about the ViewModel.</p> <p>At the moment I have this in the codebehind:</p> <pre><code>private void rtbContent_SelectionChanged(object sender, System.Windows.RoutedEventArgs e) { RichTextBox rtb = sender as RichTextBox; TextPointer tpForward = rtb.CaretPosition.GetNextContextPosition(LogicalDirection.Forward); TextPointer tpBackward = rtb.CaretPosition.GetNextContextPosition(LogicalDirection.Backward); if (tpForward != null &amp;&amp; tpBackward != null) { DependencyObject nextObj = tpForward.GetAdjacentElement(LogicalDirection.Forward); DependencyObject prevObj = tpBackward.GetAdjacentElement(LogicalDirection.Backward); TextElement textElement = (TextElement)(nextObj ?? prevObj); if (textElement != null) { tbBold.IsChecked = textElement.FontWeight == FontWeights.Bold; tbItalic.IsChecked = textElement.FontStyle == FontStyles.Italic; //... } } } </code></pre> <p>This code is formatting selected Text, toggles the state of the togglebutton when the cursor is before a formatted char/has formatted chars, just like in word...</p> <p>1.) Where does such code belong? Code-behind or ViewModel? Actually I know the answer because of technical restrictions of the RTBox it will stay in the code-behind.</p> <p>2.) The logic to toggle the format buttons is done via</p> <p></p> <p>But that will not work as the BoldCommand can not execute a method which would format the selected Text, as the selected Text of the RTB can not be bound to the ViewModel...</p> <p>How to cope with that ?</p> <p><strong>UPDATE</strong>:</p> <p><strong>ward bell</strong> said: </p> <p>"I am open to some code in the code-behind; ...I draw the line at decision logic. I smell a rat when I see a conditional statement of any kind. That’s where bugs breed. Conditional logic is code we should be testing."</p> <p>source: <a href="http://codebetter.com/blogs/wardbell/archive/2010/03/19/mvvm-josh-smith-s-way.aspx" rel="nofollow noreferrer">http://codebetter.com/blogs/wardbell/archive/2010/03/19/mvvm-josh-smith-s-way.aspx</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload