views:

268

answers:

1

I have a Silverlight DataGrid that contains a RowDetailsTemplate. The RowDetailsTemplate contains a TabControl with several TabItems. The DataGrid will be bound with items of type Contact. Contact has a property called ContactType. I would like to have several of the TabItems be hidden when ContactType is Client. Ideally I would like to do this through DataBinding but I haven't found anyway to do this yet.

+1  A: 

Bind the TabItem.Visibility in the RowDetailTemplate to the ContactType using a Value Converter that converts ContactType to Visiblity. You should add the ContactTypeConverter to the app or page as a resource.

<TabItem 
    Visibility="{Binding ContactType, Converter={StaticResource ContactTypeConverter}}"/>

namespace Demo
{
using System;
using System.Windows;
using System.Windows.Data;

public enum ContactType
{
    Client
};

/// <summary>
/// A Value converter
/// </summary>
public class ContactTypeConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var contactType = (ContactType) value;

        switch (contactType)
        {
            case ContactType.Client:
                return Visibility.Visible;

            default:
                return Visibility.Collapsed;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return DependencyProperty.UnsetValue;
    }

    #endregion
}
}
Michael S. Scherotter