user3610920
user3610920

Reputation: 1652

Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.IConvertible

I have a tab control inside which there are number of tab items.I am trying to apply multivalue converter to the visibility property of tabitem.

<TabControl>
 <TabItem
            DataContext="{Binding TabModel[3]}"
            x:Name="Tab3"
            Header="Test">
                <TabItem.Visibility>
                    <MultiBinding Converter="{StaticResource settingsvisibility}">
                        <Binding Path="UserRole"/>
                        <Binding Path="UserName"/>
                    </MultiBinding>
                </TabItem.Visibility>
                <tabView:view />
            </TabItem></tabControl>

My converter code is as follow as

 public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility visiblity = Visibility.Collapsed;
            int id = System.Convert.ToInt32(values[0]);
            string tabName = values[1].ToString();
...
....
...
}

But the values are not getting passed properly. I am getting the below exception

Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.IConvertible . Can anyone help me to get rid of this issue?

Upvotes: 1

Views: 2970

Answers (2)

Teja Reddy
Teja Reddy

Reputation: 101

if(!(values[0] is string))
    return null;

This worked for me

Upvotes: -2

user3610920
user3610920

Reputation: 1652

After i did the following check in my converter its work properly.

 if (values[0] == DependencyProperty.UnsetValue)
            {
                //do domething
            }

When the converter get called for the first time the value is being passed is

> DependencyProperty.UnsetValue

when its get called again the values are getting passed properly. so this stuff worked for me.

Upvotes: 10

Related Questions