【问题标题】:WPF Converter makes control invisibleWPF 转换器使控件不可见
【发布时间】:2016-05-27 07:40:20
【问题描述】:

我遇到了 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。有什么想法吗?

【问题讨论】:

标签: c# wpf


【解决方案1】:

这可能是因为您必须在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

【讨论】:

  • UserControl.Resources 不一样。 intellisense 告诉我标签内的 local:NumberToBestSizeConverter 的相同消息。
  • 请确保您在'local'中有正确的命名空间,并且该命名空间来自同一个程序集,否则还需要添加有关程序集的信息。
  • 我发现了问题。我将一个 double 传递给转换器,它接受了字符串,因此将 Binding.DoNothing 报告为失败。谢谢你的帮助。我不能把答案作为真正的答案,因为它不是。但我非常感谢您的帮助。
【解决方案2】:

创建转换器的实例

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

比在你的绑定中

Converter={StaticResource converterName}}"

【讨论】:

  • 谢谢。我发现真正的问题是转换器接收的是双精度而不是字符串。令人难以置信,因为它现在工作得很好,但视觉工作室现在无法渲染界面,智能感知也给了我错误。似乎是视觉工作室中的一个错误。
猜你喜欢
  • 1970-01-01
  • 2011-10-12
  • 2011-05-28
  • 1970-01-01
  • 2012-11-23
  • 2013-08-18
  • 2011-08-03
  • 1970-01-01
  • 2019-04-30
相关资源
最近更新 更多