0

I am having an issue with a WPF control I am implementing it like this

<UserControl x:Class="VizarisUpdater.Page.SetupPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:VizarisUpdater.Converters"
             mc:Ignorable="d" 
             d:DesignHeight="390" d:DesignWidth="692">

later in the page

<Label Grid.Row="2" Grid.Column="1">
  <TextBlock Text = "{Binding Path=SpaceRequired, StringFormat='Space Required: {0}', Converter={local:NumberToBestSizeConverter}}" FontSize="20" VerticalAlignment="Center"/>
</Label>

When I remove the converter it displays the number ok (without conversion) but when I add the converter it is gone. I also have intellisense telling me:

The name "NumberToBestSizeConverter" does not exists in the namespace "clr-namespace:VizarisUpdater.Converters"

I have .net Framework 4.0 defined in the project. Any ideas?

Waldo Alvarez
  • 314
  • 1
  • 11
  • 1
    examples of converter usage: http://stackoverflow.com/questions/2304269/using-value-converters-in-wpf-without-having-to-define-them-as-resources-first, http://stackoverflow.com/questions/5166705/wpf-per-binding-instance-of-a-value-converter; binding requires an instance of converter – ASh Feb 16 '16 at 06:53
  • thanks for the example pal, appreciated. – Waldo Alvarez Feb 16 '16 at 11:44

2 Answers2

2

This is probably because you have to declare your converter in XAML

<UserControl.Resources>  
 <local:NumberToBestSizeConverter x:Key="NumberToBestSizeConverter" />  
</UserControl.Resources> 

And afterwards you can use Key in your code:

<Label Grid.Row="2" Grid.Column="1">
  <TextBlock Text = "{Binding Path=SpaceRequired, StringFormat='Space Required: {0}', Converter={StaticResource NumberToBestSizeConverter}}" FontSize="20" VerticalAlignment="Center"/>
</Label>

Please note that in Converter property is used StaticResource.

madoxdev
  • 3,770
  • 1
  • 24
  • 39
  • No same thing for UserControl.Resources. intellisense is telling me the same message for local:NumberToBestSizeConverter inside the tags. – Waldo Alvarez Feb 16 '16 at 08:09
  • Please make sure that you have correct namespace in 'local', and that this namespace comes from same assembly, otherwise it is required to add information about assembly too. – madoxdev Feb 16 '16 at 08:23
  • 1
    I found the problem. I was passing a double to the converter and it accepted strings so was reporting back Binding.DoNothing as a failure. Thanks for your help. I can't put the answer as the real answer cause it wans't. But I really appreciate your help. – Waldo Alvarez Feb 16 '16 at 11:42
1

Create an instance of your converter

<Window.Resources>
    <NumberToBestSizeConverter x:Key="converterName"/>
</Window.Resources>

Than in your binding

Converter={StaticResource converterName}}"
Bohdan Biehov
  • 431
  • 4
  • 15
  • Thanks. I found the real problem it was the converter was receiving a double instead of a string. Is incredible cause it works great now but visual studio fails to render the interface now and intellisense gives me the error too. Seems a bug in visual studio. – Waldo Alvarez Feb 16 '16 at 11:43