WPF转换器使控件不可见

时间:2016-02-16 06:46:16

标签: c# wpf

我遇到了WPF控件的问题我正在实现它

<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">

在页面后面

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

当我移除转换器时,它会显示数字ok(没有转换)但是当我添加转换器时它就消失了。我也有智能感知告诉我:

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

我在项目中定义了.net Framework 4.0。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

这可能是因为您必须在XAML

中声明您的转换器
<UserControl.Resources>  
 <local:NumberToBestSizeConverter x:Key="NumberToBestSizeConverter" />  
</UserControl.Resources> 

然后您可以在代码中使用Key

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

请注意,在Converter属性中使用了StaticResource

答案 1 :(得分:1)

创建转换器的实例

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

比你的绑定

Converter={StaticResource converterName}}"