Proposed Answer change the color in ritchbox

  • 09 Mei 2012 18:41
     
      Memiliki Kode

    Hi, i tried to change the color of text in a wpf ritch box, by getting the color value from a windows forms Colordialog 

    this is the code behind:

     WpfFontColordialog wpfcolor = new WpfFontColordialog();
                Color cl = wpfcolor.opendialog();// wpfcolor:class
                MessageBox.Show("" + cl);
                TextRange tr = new TextRange(richTextB.Document.ContentStart, richTextB.Document.ContentEnd);
                tr.ApplyPropertyValue(TextElement.ForegroundProperty,cl);
    but it didn't work

Semua Balasan

  • 09 Mei 2012 20:15
     
     

    1.   Rather than it did not work, what did it do?

    2.   What is WpfFontColordialog and what does it return?

    3.   What "using" statments are in your codebehind?

    LS


    Lloyd Sheen

  • 09 Mei 2012 21:00
     
     
    WpfFontColordialog  is a class wich return the color chosen on an windows forms Colordialog: return Color
  • 09 Mei 2012 21:22
     
     

    What you need to do is create a SolidColorBrush using the color and use that  to set the TextElement.ForegroundProperty.

    LS


    Lloyd Sheen

  • 09 Mei 2012 22:30
     
     Saran Jawaban Memiliki Kode
    WpfFontColordialog  is a class wich return the color chosen on an windows forms Colordialog: return Color

    Is it of type System.Drawing.Color or System.Windows.Media.Color?
    The WinForms ColorPicker returns System.Drawing.Color type while
    WPF deals with Windows.Media.Color.
    So before creating a solidcolorbrush you need to create a
    Windows.Media.Color for example using

    var wpfColor = System.Windows.Media.Color.FromArgb (cl.A, cl.R, cl.G, cl.B);
    tr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(wpfColor));




  • 11 Mei 2012 17:03
     
     

     Thank you,

    it is System.Windows.Media.Color;

    i tried this code , it work but:

    =>the red change to blue

    the blue change to red

    also, it change all of the text not only the selected part

  • 11 Mei 2012 20:55
     
      Memiliki Kode
    It's your code that defines the complete document range,
    and did you mention selection? :-).

    If you want the selection foreground to change
    It's basically:

    richTextBox1.Selection.ApplyPropertyValue (TextElement.ForegroundProperty, new SolidColorBrush(wpfColor));

    You have, however, to clear the color when
    the selection has changed.

    Chris


  • 11 Mei 2012 22:45
     
     
    yes i tried this but the problem was, the blue color is considred as a red!!
  • 11 Mei 2012 23:04
     
     

    Not sure what you mean by that.  Are you returning a blue (0,0,255) from the dialog.  

    Did it work with the entire set of text?

    LS


    Lloyd Sheen

  • 11 Mei 2012 23:22
     
     

    I wrote a small sample app and I can find no problem with any of the code that has been presented here.

    Both the entire contents (as you started) or the Selection both work with the info pulled from the ColorDialog.

    In reality the only thing funky about all this is the color conversions made necessary by the terrible (IMHO) implementation of color in dot.net and the fact that the WPF team saw fit not to create WPF common dialogs.

    Lloyd Sheen


    Lloyd Sheen

  • 12 Mei 2012 8:15
     
     
    Make sure that in WpfFontColorDialog  the conversion from WinForms color
    to WPF color is appropriate.
    If R and B are interchanged, there's some spot in code that is
    responsible.

    Chris