I have custom control in wpf which change his look based on one Property:
…
<Grid>
<Rectangle Fill="[Something]" />
</Grid>
In code i have the property AlarmLevel, when AlarmLevel is bigger than 5 I want the fill to be red, otherwise blue.
How can I do this. (I don’t want the fill property to be exposed)
Tnx
Since you’re basing your fill value on an inequality, you could do this a couple of ways.
The recommended way is probably to use a converter on your binding to make it into a boolean value. Then use a data trigger to set the fill value based on whether the value is true or false, like so:
Your converter could look something like (perhaps with more exception handling):
Don’t forget you’ll need to add a reference to the converter class as a resource on your user control:
If you wanted to forego the converter method, you could also create a “helper” boolean property in your data context called something like “IsAlarming”. It would look something like:
You would then bind your data trigger to IsAlarming rather than AlarmLevel. This isn’t recommended though, because it’s not pure MVVM.