Change the visibility of child controls in a RowDetailsTemplate based on the DataContext property
I have a Silverlight DataGrid that contains a RowDetailsTemplate. The RowDetailsTemplate contains a TabControl with several TabItems. The DataGrid will be associated with elements of type Contact. The contact has a ContactType property. I would like some of the TabItems to be hidden when the ContactType is a client. Ideally I would like to do this via DataBinding, but I haven't found it yet.
+2
source to share
1 answer
Bind the TabItem.Visibility in the RowDetailTemplate to the ContactType using a value converter that converts the ContactType to Visiblity. You must add the ContactTypeConverter to your application 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
}
}
+1
source to share