Binding Textblock Style/Converter?

You can do this with Triggers only, there's a bit of redundancy though.

Up vote 0 down vote favorite share g+ share fb share tw.

I have: OrderType is a string can potentially be A,B,C,D. But I want the textblock to display YES if (A or B) and NO if (C or D), but want the foreground color to be Green if A, Blue if B, Yellow if C, Red if D. What would be the best way to do this?

I'm a little confused if I use a converter, what the style trigger should bind to or if there is a better approach. Thanks. Wpf xaml data-binding mvvm link|improve this question edited Jan 19 at 17:07H.B.43k61645 asked Jan 19 at 17:06thevillain931.

You should use an IValueConverter for this purpose: public class OrderTypeToYesNoConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if(value! = null) { string val = value.ToString(); if(val == "A" || val == "B") return "YES"; } return null; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } XAML.

I object to the "should"... – H.B. Jan 19 at 17:16 @H.B. Fine, object it, but tell us why =) The converter approach is very reusable, which is why I tend to advise to use them instead of triggers. – Baboon Jan 19 at 17:17 Converters are obviously for conversions, like boolean to visibility. For something like this where there are just multiple cases (like a switch) a converter seems like abuse to me.

– H.B. Jan 19 at 17:19 @H.B. I've just shown an example, but you can make them more generic, by using the "parameter" parameter. I've seen people make their own custom script language through it, and it reduces the XAML code a lot. You also get a better shot at debugging!

– Baboon Jan 19 at 17:21 There is no intrinsic value in reducing XAML, script languages are imperative, which goes completely against what XAML is all about: declarative markup. – H.B. Jan 19 at 17:23.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions