WPF StringFormat Designer错误

时间:2014-10-13 13:26:37

标签: wpf

我制作了一个MultiBind Converter,将TextBlock格式化为模板。为用户创建了一个依赖项属性“StringFormat”,以定义文本在TextBlock中显示的格式。另一个属性(Value)已存在于父对象中。代码如下:

** XAML **

<TextBlock
    x:Name="myText">
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource StringFormatConverter}">
            <Binding Path="Value" RelativeSource="{RelativeSource TemplatedParent}"/>
            <Binding Path="StringFormat" RelativeSource="{RelativeSource TemplatedParent}"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

** C#**

public class StringFormatConverter : IMultiValueConverter
{
    public object Convert(
        object[] value,
        Type targetType,
        object parameter,
        CultureInfo culture)
    {
        string val = string.Empty;

        if (value[1] != null)
        {
            val = (string)(value[1].ToString());
            return String.Format(culture, val, value[0]);
        }
        else return value[0];
    }

    public object[] ConvertBack(
        object value,
        Type[] targetTypes,
        object parameter,
        CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

当我运行应用程序时,它运行没有错误,传递的格式字符串按预期工作。但是,在设计器模式下,它会显示以下消息:

  

XamlParseException:输入字符串的格式不正确。

     

栈跟踪

     

在System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader)   templateReader,XamlObjectWriter currentWriter)at   System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter   objectWriter)at   System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject的   container,IComponentConnector componentConnector,IStyleConnector   styleConnector,列出1 affectedChildren, UncommonField 1   templatedNonFeChildrenField)at   System.Windows.FrameworkTemplate.LoadContent(DependencyObject的   容器,列出1 affectedChildren) at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField 1   dataField,DependencyObject容器,FrameworkElementFactory   templateRoot,Int32 lastChildIndex,HybridDictionary   childIndexFromChildID,FrameworkTemplate frameworkTemplate)at   System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1   templateDataField,FrameworkElement容器)at   System.Windows.FrameworkElement.ApplyTemplate()

     

InnerException:输入字符串的格式不正确。

     

FormatException:输入字符串的格式不正确。

     

栈跟踪

     

在System.Text.StringBuilder.AppendFormat(IFormatProvider提供程序,   字符串格式,Object [] args)at   System.String.Format(IFormatProvider提供程序,String格式,Object []   args)at   Ipiranga.WPF.Componentes.Componentes.StringFormatConverter.Convert(对象[]   value,Type targetType,Object parameter,CultureInfo culture)at   System.Windows.Data.MultiBindingExpression.TransferValue()at   System.Windows.Data.MultiBindingExpression.Transfer()at   System.Windows.Data.MultiBindingExpression.UpdateTarget(布尔   includeInnerBindings)at   System.Windows.Data.MultiBindingExpression.AttachToContext(布尔   lastChance)at   System.Windows.Data.MultiBindingExpression.AttachOverride(DependencyObject的   d,DependencyProperty dp)at   System.Windows.Data.BindingExpressionBase.OnAttach(DependencyObject d,   DependencyProperty dp)at   System.Windows.StyleHelper.GetInstanceValue(UncommonField 1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry) at System.Windows.StyleHelper.GetChildValueHelper(UncommonField 1   dataField,ItemStructList 1& valueLookupList, DependencyProperty dp, DependencyObject container, FrameworkObject child, Int32 childIndex, Boolean styleLookup, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot) at System.Windows.StyleHelper.GetChildValue(UncommonField 1 dataField,   DependencyObject容器,Int32 childIndex,FrameworkObject子句,   DependencyProperty dp,FrugalStructList 1& childRecordFromChildIndex, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot) at System.Windows.StyleHelper.GetValueFromTemplatedParent(DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList 1&amp; childRecordFromChildIndex,   FrameworkElementFactory templateRoot,EffectiveValueEntry&amp;进入)
  在   System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject的   容器,FrameworkObject子,Int32 childIndex,   FrugalStructList 1& childRecordFromChildIndex, DependencyProperty dp, FrameworkElementFactory templateRoot) at System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList 1安培; childRecordFromChildIndex,Boolean isDetach,   FrameworkElementFactory templateRoot)at   System.Windows.FrameworkTemplate.InvalidatePropertiesOnTemplate(DependencyObject的   容器,对象currentObject)at   System.Windows.FrameworkTemplate.HandleBeforeProperties(对象   createdObject,DependencyObject&amp; rootObject,DependencyObject   容器,FrameworkElement feContainer,INameScope nameScope)at   System.Windows.FrameworkTemplate&LT;&GT; c__DisplayClass6.b__3(对象   sender,XamlObjectEventArgs args)at   System.Xaml.XamlObjectWriter.OnBeforeProperties(Object value)at   System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext   ctx)在System.Xaml.XamlObjectWriter.WriteStartMember(XamlMember)   System.Xaml.XamlWriter.WriteNode(XamlReader reader)

我做错了什么,因为代码运行正常?提前谢谢!

1 个答案:

答案 0 :(得分:0)

我必须猜测你的安全检查是不好的。不要检查值[x]!= null,使用as关键字:

string strVal = value[1] as string;

原因是你可能没有得到null,而是DependencyProperty.UnsetValue。