1
votes

I have this problem. In XAML I use multi converter on textblock. I bind 4 string properties on textblock.

XAML code look like this:

        <TextBlock Style="{StaticResource InfosStyle}"
                   TextWrapping="Wrap"
                   >
                   <TextBlock.Text>
                       <MultiBinding Converter="{StaticResource statusInfoConvertor}">
                           <Binding Path="StatusInfo.IsLogged"  Mode="OneWay" UpdateSourceTrigger="PropertyChanged"/>
                           <Binding Path="StatusInfo.IsChating" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"/>
                           <Binding Path="StatusInfo.RoomName"  Mode="OneWay" UpdateSourceTrigger="PropertyChanged"/>
                           <Binding Path="StatusInfo.LastLogin" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"/>
                       </MultiBinding>
                   </TextBlock.Text>
        </TextBlock>

Converter look like this:

public class StatusInfoConverter : IMultiValueConverter
{

    #region Implementation of IMultiValueConverter

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        bool isChatting=false, isLogged=false;


        if (!string.IsNullOrEmpty(values[0].ToString()) && !string.IsNullOrWhiteSpace(values[0].ToString()))
            isLogged = System.Convert.ToBoolean(values[0].ToString());

        if (!string.IsNullOrEmpty(values[1].ToString()) && !string.IsNullOrWhiteSpace(values[1].ToString()))
            isChatting = System.Convert.ToBoolean(values[1].ToString());

        if (isLogged)
        {
            if (isChatting)
            {
                return string.Format(new CultureInfo("sk-Sk"), "Chatuje\nMiestnosť: {0}", values[2]);
            }
            return string.Format(new CultureInfo("sk-SK"), "Iba prihlásení");
        }

        return string.Format(new CultureInfo("sk-Sk"), "Offline");
    }

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

    #endregion
}

Problem is I get in designer this error:

System.FormatException String was not recognized as a valid Boolean. at System.Boolean.Parse(String value) at System.Convert.ToBoolean(String value) at Spirit.Converters.StatusInfoConverter.Convert(Object[] values, Type targetType, Object parameter, CultureInfo culture) in C:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger\ver.beta\Pokec__Messenger\Spirit_v1.2\Converters\Convertors.cs:line 160 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.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance) at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance) at MS.Internal.Data.DataBindEngine.Run(Object arg) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

On line 160 is:

159.            if (!string.IsNullOrEmpty(values[0].ToString()) && !string.IsNullOrWhiteSpace(values[0].ToString()))
160.                isLogged = System.Convert.ToBoolean(values[0].ToString());

How can I solve this problem. If I compile this code and test app, it works..but I have problem with shoe view in designer because I bind empty string varibale on texblock and also use converter.

2

2 Answers

0
votes

A couple of ideas ...

1) Use design time data so that you have a valid source for your bindings. See:

http://karlshifflett.wordpress.com/2009/10/28/ddesigninstance-ddesigndata-in-visual-studio-2010-beta2/

2) Add some design-time only logic to your converter, that handles the empty string. See the following for detecting design-time:

http://alanle.com/2009/02/10/design-time-check-for-wpf-and-silverlight/

Colin E.

0
votes

I don't really think Colin's answer is correct. You have a bug in your converter code, plain and simple. You are doing values[0].ToString() without checking if values[0] is null first. That has nothing to do with design time. It will crash in runtime too.

Instead, do:

string strValue0 = value[0] as string; string strValue1 = value[1] as string;

Then do the IsNullOr checks...