Wednesday, October 16, 2013

WPF Converter in Resource Dictionary

Using converter is daily, in as general we doing that like this:

1. Define a c# ValueConverter

2. Set Xaml namespace like this :

xmlns:converters="clr-namespace:X.Y.Converters;assembly=X.Y"

3. set local resources with key like this:

<UserControl.Resources>
   <converters:BoolToVisibilityConverter x:Key="boolToVisibilityConverter" />         
/UserControl.Resources>

4. just using the key inside your document where is needed:

< MediaElement Visibility="{Binding Path=X.Visibility, Converter={StaticResource boolToVisibilityCollapsedConverter}}"  />
But, inside Resource dictionary file when you need some Converter you cannot use local resource as Static Resources, so what you need is either Global static Converters allocate in your App.Xaml , or simply using the next xaml syntax:

1. add Xaml namespace like this :

xmlns:localConverters="clr-namespace:X.Resources"

2. and use Binding Converter syntax ..

 
<Setter Property="FontWeight">
  <Setter.Value>
    <Binding Path="(XValue)">
      <Binding.Converter>
        <localConverters:BoolToBoldConverter />
      </Binding.Converter>


QR: Inline images 1