Waldo Alvarez
Waldo Alvarez

Reputation: 312

WPF Converter makes control invisible

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?

Upvotes: 0

Views: 77

Answers (2)

Bohdan Biehov
Bohdan Biehov

Reputation: 431

Create an instance of your converter

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

Than in your binding

Converter={StaticResource converterName}}"

Upvotes: 1

madoxdev
madoxdev

Reputation: 3890

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.

Upvotes: 2

Related Questions