user3952846
user3952846

Reputation: 141

WPF StringFormat Designer error

I made a MultiBind Converter to format a TextBlock into a template. One dependency property "StringFormat" was created for the user to define the format the text will appear in the TextBlock. The other property (Value) already exists in the parent object. The codes are as follows:

** 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();
    }
}

When I run the application, it runs without errors, and the passed format string works as expected. However, in designer mode, it displays the following message:

XamlParseException: Input string was not in a correct format.

StackTrace

at 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, List1 affectedChildren, UncommonField1 templatedNonFeChildrenField) at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List1 affectedChildren) at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate) at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container) at System.Windows.FrameworkElement.ApplyTemplate()

InnerException: Input string was not in a correct format.

FormatException: Input string was not in a correct format.

StackTrace

at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) at System.String.Format(IFormatProvider provider, String format, Object[] args) at Ipiranga.WPF.Componentes.Componentes.StringFormatConverter.Convert(Object[] 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(Boolean includeInnerBindings) at System.Windows.Data.MultiBindingExpression.AttachToContext(Boolean 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(UncommonField1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry) at System.Windows.StyleHelper.GetChildValueHelper(UncommonField1 dataField, ItemStructList1& valueLookupList, DependencyProperty dp, DependencyObject container, FrameworkObject child, Int32 childIndex, Boolean styleLookup, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot) at System.Windows.StyleHelper.GetChildValue(UncommonField1 dataField, DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList1& childRecordFromChildIndex, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot) at System.Windows.StyleHelper.GetValueFromTemplatedParent(DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList1& childRecordFromChildIndex, FrameworkElementFactory templateRoot, EffectiveValueEntry& entry)
at System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList1& childRecordFromChildIndex, DependencyProperty dp, FrameworkElementFactory templateRoot) at System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList1& childRecordFromChildIndex, Boolean isDetach, FrameworkElementFactory templateRoot) at System.Windows.FrameworkTemplate.InvalidatePropertiesOnTemplate(DependencyObject container, Object currentObject) at System.Windows.FrameworkTemplate.HandleBeforeProperties(Object createdObject, DependencyObject& rootObject, DependencyObject container, FrameworkElement feContainer, INameScope nameScope) at System.Windows.FrameworkTemplate.<>c__DisplayClass6.b__3(Object sender, XamlObjectEventArgs args) at System.Xaml.XamlObjectWriter.OnBeforeProperties(Object value) at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx) at System.Xaml.XamlObjectWriter.WriteStartMember(XamlMember property) at System.Xaml.XamlWriter.WriteNode(XamlReader reader)

What am I doing wrong, since the code runs and works properly? Thanks in advance!

Upvotes: 0

Views: 685

Answers (1)

SledgeHammer
SledgeHammer

Reputation: 7736

I'd have to guess that your safety check is bad. Don't check for value[x] != null, use the as keyword:

string strVal = value[1] as string;

Reason being is you probably aren't getting null, but rather DependencyProperty.UnsetValue.

Upvotes: 0

Related Questions