Thursday 18 February 2010

Remove padding from TextBox in WPF

If you add the attribute

Padding="0, 0, 0, 0"

to most WPF elements e.g. <Label></Label>, the text is placed in the top left hand corner with no space between the left and top sides.  However if you try it with a TextBox element it appears to have no effect.

Fortunately as a workaround you can set the padding to have negative values.  So e.g. if I want a standard TextBox element, with standard fontsize, with no space above the text, doing something like:

Padding="0, -2, 0, 0"

Will have the desired effect.

Wednesday 3 February 2010

Select elements by innerHTML with jQuery

Sometimes it is necessary to use jQuery to select html elements with a certain innerHTML value.  Consider the following 3 labels:

<label>apples</label>
<label>oranges</label>

<label>opal fruits</label>

Apparently the following are all equivalent ways of matching a label based on its innerHTML value:

$("label[innerHTML=opal fruits]") - no quotes
$("label[innerHTML='opal fruits']") - single quotes within double quotes
$("label[innerHTML=\"opal fruits\"]") - escaped double quotes within double quotes


However innerHTML must be a strange one, as this form of matching only works for strings of multiple words and without the quotes.  As soon as you try and match a single word, or put quotes around the string, no matches are found.  Even more confusing is that if you try the following, which should match any element with innerHTML, you get no matches:

$("[innerHTML]")

Fortunately, jQuery, as you might expect, has a better way of doing it using the :contains(text) selector.  So now I can use the following:

$("label:contains(opal fruits)")