WPF user control

  • Thread starter Thread starter Vivien Parlat
  • Start date Start date
V

Vivien Parlat

Hello,

I hope i'm posting into the good group, i found no active group around
xaml.

My situation is the following:

I'm trying to play around xaml in the new VC# Express 2008. My
"aim" (i imagine that when i'll succeed this aim, my understanding
will be much better than now) is to create a personalized textbox,
whose background varies between : Blue (value <= 0), Yellow (value
between and 100), Red (value >= 100) or Orange (non-numeric value).

To do so I designed a UserControl, containing a TextBox (no way to
inherit from it in xaml ?), storing a converted int? value ("?"
because of cases user enters nonnumerical value), and exposing
DependancyProperties like "Hot", "Cold", and so on.

The code is as minimal as posible, because I want most of the work to
be done in another control, say "ColoredTextBox", which I want formed
as the TextBoxes in example:
http://msdn.microsoft.com/msdnmag/c.../issues/07/01/Foundations/Default.aspx?loc=fr

Initially I couldn't only see the exposed property; now, moreover, I
have errors like "Error 1 Operation is not valid due to the current
state of the object." on a line such as
<ControlTemplate x:Key="templateTextBox"
TargetType="{x:Type my:MyNumTextBox}">,
and globally I cannot find the way to create this textbox which uses
triggers on my custom properties to change the background colors.

Can anyone tell me if the way i try to do it is wrong, or how to
achieve this little control ? My only need is that it is made using
WPF, because the interest for me is to learn it, I think I could do it
easily using WinForms.

Thank you in advance (and sorry for my English) !
 
Not sure how to do it in XAML but you could suscribe to the textbox's
text changed event, then write some procedural code to change the
background of the textbox depending on its contents.
 
Not sure how to do it in XAML but you could suscribe to the textbox's
text changed event, then write some procedural code to change the
background of the textbox depending on its contents.

Thanks for your answer, but that's what I was talking about when
saying "I think I could do it
easily using WinForms". This doesn't look like the xaml way, like in
the link. This is that way i'm trying to 'mimic'.
 
Hello,
I'm trying to play around xaml in the new VC# Express 2008. My "aim"
(i imagine that when i'll succeed this aim, my understanding will be
much better than now) is to create a personalized textbox, whose
background varies between : Blue (value <= 0), Yellow (value between
and 100), Red (value >= 100) or Orange (non-numeric value).

The best way to achieve this is to establish a data binding on the text-box's
Background property, binding it to the Text property of the same control
(RelativeSource pointing to self, Path pointing to Text), with a custom Converter
that converts Text into a Brush. The converter should try converting the
text into an int, and then construct a SolidColorBrush, as appropriate.

If you'd like to avoid copypasting the binding to all the textboxes around,
you could inherit a class from a TextBox (no XAML involved) and wire up the
same binding in its constructor thru the SetBinding method.

(H) Serg
 
Hello,


The best way to achieve this is to establish a data binding on the text-box's
Background property, binding it to the Text property of the same control
(RelativeSource pointing to self, Path pointing to Text), with a custom Converter
that converts Text into a Brush. The converter should try converting the
text into an int, and then construct a SolidColorBrush, as appropriate.

If you'd like to avoid copypasting the binding to all the textboxes around,
you could inherit a class from a TextBox (no XAML involved) and wire up the
same binding in its constructor thru the SetBinding method.

(H) Serge

Many thanks Serge, I wasn't taking this way before your answer. It
works fine, with not much code. I think I will embed this customized
textbox into a CustomControl to keep its xaml nature 'clean'.
 
Hello,
Many thanks Serge, I wasn't taking this way before your answer. It
works fine, with not much code. I think I will embed this customized
textbox into a CustomControl to keep its xaml nature 'clean'.

I think a CustomControl is even too much code. Just a class that inherits
from TextBox and sets the binding in its ctor. With a custom control, you'll
have to expose all of the TextBox's properties to make it usable.

(H) Serg
 
Hello,


I think a CustomControl is even too much code. Just a class that inherits
from TextBox and sets the binding in its ctor. With a custom control, you'll
have to expose all of the TextBox's properties to make it usable.

(H) Serge

I think I will follow your advice. I tried to do it using xaml, and I
am still experiencing problems with using my Converter class from same
assembly, VC#2008express seems to be buggy about that...
 
Back
Top