There are several value converters that you can use to bind your view to model.

The most useful one is VisibilityConverter. It can be used with any types of model properties. It converts "zero" to Hidden and non "zero" to Visible. It can also invert its behavior so you can hide one element and display another one.

<Grid>
    <TextBlock Text="no results"
               Visibility="{Binding SearchResults.Count, Converter={StaticResource VisibilityConverter}, ConverterParameter=invert}"/>

    <ListView ItemsSource="{Binding SearchResults}"
               Visibility="{Binding SearchResults.Count, Converter={StaticResource VisibilityConverter
"/>
</Grid>
}}
It supports numbers, strings, object references, booleans and Visibility.

If your element has a boolean property then you can do the same with BooleanConverter.

If you need to calculate a simple expression then you can use EvaluatorConverter:
<Border Height="40" Background="Red"
        Width="{Binding Level, Converter={StaticResource EvaluatorConverter}, ConverterParameter=40*$1}"/>

Here the width of the border will be Level multiplied by 40.
  • The expression is specified through the ConverterParameter.
  • $1 is used as the binding value placeholder.
  • You can use a limited set of functions in the expression since it is implemented using a XSLT. Some string, numeric, boolean and date/time functions are available (please check the XPath function reference). Here are some examples:
    • string($1)
    • concat('prefix', '$1', 'postfix')
    • number($1)
    • abs($1)
    • ceiling()
    • floor($1)
    • round($1)
    • string-join(('title', '$1'),'-')
    • substring(string,start,len)
    • substring(string,start)
    • string-length(string)
    • upper-case(string)
    • lower-case(string)
    • replace(string,pattern,replace)
    • boolean(arg)
    • not(arg)
...

Last edited Jan 24, 2013 at 5:18 AM by mglukhankov, version 1

Comments

No comments yet.