c# - Problems with RichTextBox.Selection and TextSelection.ApplyPropertyValue -


i'm trying create text editor in wpf using richtextbox. problem changing font size of text. code works intended in every case, except when cursor inside word. in case should not change font size of anything, except font size of text come if user writes anything. problem reason textselection.applypropertyvalue(richtextbox.fontsizeproperty, value) changes font size of whole word when cursor inside word.

this eventhandler:

private void fontsizebox_selectionchanged(object sender, selectionchangedeventargs e) {     combobox combobox = (combobox)sender;     string value = (string)combobox.selectedvalue;      if (combobox.isdropdownopen)     {         textselection text = textboxmain.selection;         richtextbox.focus();         text.applypropertyvalue(richtextbox.fontsizeproperty, value);     }             } 

and cannot use adding !text.text.isempty inside if statement, because still need able change font size text written.

i have found similar questions on stackoverflow, none actual working answer.

edit: added xaml

<window x:class="mathedit.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns:local="clr-namespace:mathedit"     mc:ignorable="d"     title="mainwindow" height="350" width="525"> <window.commandbindings>     <commandbinding command="open" executed="opencommandbinding_executed"></commandbinding>     <commandbinding command="save" executed="savecommandbinding_executed"></commandbinding>     <commandbinding command="saveas" executed="saveascommandbinding_executed"></commandbinding> </window.commandbindings>  <window.inputbindings>     <keybinding key="o" modifiers="control" command="open"></keybinding>     <keybinding key="s" modifiers="control" command="save"></keybinding>     <keybinding key="s" modifiers="control+alt" command="saveas"></keybinding> </window.inputbindings>  <dockpanel>     <menu dockpanel.dock="top">         <menuitem header="_file">             <menuitem header="_new" inputgesturetext="ctrl+n" />             <menuitem header="_open" inputgesturetext="ctrl+o" command="open"/>             <menuitem header="_save" inputgesturetext="ctrl+s" command="save"/>             <menuitem header="_save as" inputgesturetext="ctrl+alt+s" command="saveas"/>             <separator />             <menuitem header="_exit" inputgesturetext="alt+f4" />         </menuitem>         <menuitem header="_tools">             <menuitem header="_check if toby = on" ischeckable="false" ischecked="true" click="menuitem_click" />             <menuitem header="_settings" click="menuitem_click_2" ischeckable="true" />             <menuitem header="_add formula" x:name="menuitemadd" click="menuitem_add_click" />         </menuitem>         <combobox x:name="fontsizebox" width="40" selectedvaluepath="content" selectionchanged="fontsizebox_selectionchanged" selectedindex="2">             <comboboxitem content="5"/>             <comboboxitem content="12"/>             <comboboxitem content="16"/>             <comboboxitem content="20"/>         </combobox>     </menu>     <grid x:name="gridparent">     <richtextbox x:name="richtextbox" acceptsreturn="true" selectionchanged="textboxmain_selectionchanged" />     </grid> </dockpanel> 

this simple application of property value no-text selection not possible. @ when apply real selection: have text "hello brave new world". document inside rtb looks (simplified)

<flowdocument>    <paragraph>      <run>hello brave new world</run>    </paragraph>  </flowdocument>

when select "brave" , change font size (or else), document changes

<flowdocument>    <paragraph>      <run>hello </run>      <run fontsize="20">brave</run>      <run> new world</run>    </paragraph>  </flowdocument>

an attribute has applied text; there no way change "nothing", requested. if want achieve me looks closest requirement, have split document - when selection empty - , create empty run desired font size. above example (assuming, caret positioned before "brave"):

<flowdocument>    <paragraph>      <run>hello </run>      <run fontsize="20"></run>      <run>brave new world</run>    </paragraph>  </flowdocument>

as leaves endless possibilities end infinite number of "empty" runs, advise revise requirement.

hope helps (at least little bit)

jürgen


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -