c# - TextBox.LineCount always -1 WPF -


i created textbox code behind this:

    textbox txtplaintxt = new textbox();     txtplaintxt.height = 200;     txtplaintxt.width = 300;     txtplaintxt.textwrapping = textwrapping.wrap;     txtplaintxt.text = text;     int linecount = txtplaintxt.linecount; 

i'm trying linecount property of textbox problem has value of "-1". guess have created code behind , not in xaml becouse when create exact same textbox in xaml, works fine , correct number of lines in it. tried call updatelayout() method , tried call focus() method this:

txtplaintxt.focus(); txtplaintxt.updatelayout(); txtplaintxt.focus(); txtplaintxt.updatelayout(); 

but still value of -1. how can solve problem?

that happening because until layout gets measured, don't have actualheight , actualwidth, linecount can't calculated until happens.

that means can use linecount property after layout got measured & arranged.

(updatelayout() notifies layout engine layout should updated , returns.)

public partial class window1 : window {     textbox txtplaintxt = new textbox();      public window1()     {         txtplaintxt.height = 200;         txtplaintxt.width = 300;         txtplaintxt.textwrapping = textwrapping.wrap;         txtplaintxt.text = "some text text text text text";          grid.setrow(txtplaintxt, 0);         grid.setcolumn(txtplaintxt, 0);         gridmain.children.add(txtplaintxt);          // here -1         int linecount = txtplaintxt.linecount;          gridmain.layoutupdated += new eventhandler(gridmain_layoutupdated);         txtplaintxt.layoutupdated += new eventhandler(txtplaintxt_layoutupdated);      }      void txtplaintxt_layoutupdated(object sender, eventargs e)     {         // layout updated, linecount have value         int linecount = txtplaintxt.linecount;     }      void gridmain_layoutupdated(object sender, eventargs e)     {         // here correct         int linecount = txtplaintxt.linecount;     } } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -