1

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?

user3610920
  • 1,582
  • 2
  • 13
  • 24
  • set a breakpoint and check what is really stored in `values[0]` - likely not IConvertible – ASh Apr 09 '18 at 09:26
  • if there is smth sensitive on those hidden TabItems, which should not be shown to certain roles - users still can access it if use Snoop for example to change Visibility forcibly – ASh Apr 09 '18 at 09:30
  • You should consider binding data and templating it into the tabitems rather than your current mechanism. Whatever logic you have in the converter would then go into deciding which items to put in the observablecollection you bind to itemssource of the tabcontrol. – Andy Apr 09 '18 at 10:10

2 Answers2

8

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.

user3610920
  • 1,582
  • 2
  • 13
  • 24
  • Is it possible to check Design-Time like `DesignerProperties.GetIsInDesignMode(new DependencyObject())` inside a Converter? I have the issue only inside my designer and NOT at runtime – KargWare Dec 04 '22 at 15:04
-2

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

This worked for me

Teja Reddy
  • 91
  • 1
  • 3
  • 1
    Please have a proper description of your answer, and not just . Good / great answers should indicate why it has failed for the asking person, how did your proposal address that failure and what was the solution – Mavi Domates Jun 19 '20 at 11:08