views:

19

answers:

1

This is my current XAML.

<StackPanel Orientation="Horizontal" Grid.Column="3" Grid.Row="1">
    <Label Content="Allocated:" FontSize="14"/>
    <Label Content="{Binding AllocatedUnits, Mode=OneWay, ValidatesOnDataErrors=True}" ContentStringFormat="N0" FontSize="14"/>
</StackPanel>

How would I change this so that the red validation rectangle is around the whole text instead of just the number. (I will accept throwing away the stack panel entirely and doing something else.

+2  A: 

A string-formatted binding would probably do the trick in this case, but that wasn't available in .NET 3.0 (in case you're still using that version!). If you can use it, you'd only need a single label control (so you can ditch both the other label and the stackpanel, and your validation border will wrap all the text in the remaining label).

EDIT: as per Jonathan's comment, it seems you need two attributes to do this on a content control...

Use something like this for your binding:

Content="{Binding AllocatedUnits, ValidatesOnDataErrors=true}" ContentStringFormat="Allocated: {0}"

MSDN documentation is here.

Dan Puzey
Since it is label, you have to use ContentStringFormat instead of StringFormat. Aside from that, it works great.
Jonathan Allen
Thanks Jonathan - I've changed my answer to suit.
Dan Puzey
Oh no, ContentStringFormat isn't a property on Binding. (That would make too much sense.) Instead, it is a property on the Label itself.
Jonathan Allen
See here for more info: http://stackoverflow.com/questions/3826761/wpf-stringformat-problems-with-a-label
Jonathan Allen
Ew, that really is horrible! Will edit...
Dan Puzey