【发布时间】:2011-03-04 09:47:51
【问题描述】:
我遇到了 WPF 中的数据绑定问题。
当我尝试使用值转换器并将 NotifyOnTargetUpdated=True 属性设置为 True 时,我收到带有以下消息的 XamlParseException:
'System.Windows.Data.BindingExpression' 值不能分配给属性 对象的“连续” 'View.UserControls.ShadowedText'。 值不能为空。参数名称: 对象处的 textToFormat 错误 'System.Windows.Data.Binding' 在 标记文件 'View.UserControls;component/saletotal.xaml' 第 363 行第 95 位。
绑定非常标准:
<my:ShadowedText Contenu="{Binding Path=Total,
Converter={StaticResource CurrencyToStringConverter},
NotifyOnTargetUpdated=True}"
TargetUpdated="MontantTotal_TargetUpdated">
</my:ShadowedText>
(为简洁起见删除了样式属性)
转换器存在于资源中,并且当 NotifyOnTargetUpdated=True 被移除时可以正常工作。类似地,TargetUpdated 事件被正确调用和实现,并在转换器被移除时工作。
注意:此绑定在 ControlTemplate 中定义,但我认为这与问题无关。
谁能解释一下发生了什么?我定义绑定错误吗?这些功能是否相互排斥(在这种情况下,您能解释一下为什么会这样)吗?
提前致谢。
更多信息:这是 TargetUpdated 处理程序的内容:
private void MontantTotal_TargetUpdated(object sender, DataTransferEventArgs e)
{
ShadowedText textBlock = (ShadowedText)e.TargetObject;
double textSize = textBlock.Taille;
double delta = 5;
double defaultTaille = 56;
double maxWidth = textBlock.MaxWidth;
while (true)
{
FormattedText newFormat = new FormattedText(textBlock.Contenu,
CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
new Typeface("Calibri"), textSize,
(SolidColorBrush) Resources["RougeVif"]);
if (newFormat.Width < textBlock.MaxWidth && textSize <= defaultTaille)
{
if ((Math.Round(newFormat.Width) + delta) >= maxWidth || textSize == defaultTaille)
{
break;
}
textSize++;
}
else
{
if ((Math.Round(newFormat.Width) - delta) <= maxWidth && textSize <= defaultTaille)
{
break;
}
textSize--;
}
}
textBlock.Taille = textSize;
}
处理程序的作用是根据内容的长度调整控件的大小。这很丑陋,但我希望在重构之前让功能部分正常工作。
【问题讨论】:
-
你在 TargetUpdated 事件处理程序中做了什么特别的事情吗?
-
我不知道这是否只是一个错字,但您在绑定中缺少逗号。它应该如下所示:{Binding Path=Total, Converter={StaticResource CurrencyToStringConverter}, NotifyOnTargetUpdated=True}
-
这是一个错字,为了便于阅读,我在问题中添加了换行符,在真正的源代码中 Converter 和 NotifyOnTargetUpdated 部分之间有一个逗号。编辑问题以解决此问题。
-
我在问题中添加了处理程序的代码。虽然它过于复杂,但我认为它不会做任何可能破坏绑定的事情。
标签: wpf data-binding exception ivalueconverter