Skip to content

Instantly share code, notes, and snippets.

@janvanderhaegen
Created January 22, 2014 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janvanderhaegen/8559597 to your computer and use it in GitHub Desktop.
Save janvanderhaegen/8559597 to your computer and use it in GitHub Desktop.
Colors the background of a control.
public static IEnumerable<IContentItemPresenter> GetAssociatedPresenters(this IContentItem contentItem)
{
var internale = contentItem as IContentItemInternal;
if (internale == null)
return new List<IContentItemPresenter>();
return internale.AssociatedPresenters;
}
public static void SetBackground(this IContentItemPresenter presenter, Color color)
{
if (presenter.Visual != null)
{
Control el = presenter.Visual as Control;
//{System.Windows.Controls.Primitives.date}
TextBox tb = FindControlByType<TextBox>(el);
if (tb != null)
{
Border tbContent = FindControlByType<Border>(tb);
if (tbContent != null)
{
tbContent.Background = new SolidColorBrush(color);
}
}
if (el is DateTimePickerVisual)
{
TimeUpDown tu = FindControlByType<TimeUpDown>(el);
TextBox tbo = FindControlByType<TextBox>(tu);
Grid gr = FindControlByType<Grid>(tbo);
Border bo = FindControlByType<Border>(gr);
bo.Background = new SolidColorBrush(color);
}
}
}
private static T FindControlByType<T>(DependencyObject container) where T : DependencyObject
{
return FindControlByType<T>(container, null);
}
private static T FindControlByType<T>(DependencyObject container, string name) where T : DependencyObject
{
T foundControl = null;
//for each child object in the container
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(container); i++)
{
//is the object of the type we are looking for?
if (VisualTreeHelper.GetChild(container, i) is T && (VisualTreeHelper.GetChild(container, i).GetValue(FrameworkElement.NameProperty).Equals(name) || name == null))
{
foundControl = (T)VisualTreeHelper.GetChild(container, i);
break;
}
//if not, does it have children?
else if (VisualTreeHelper.GetChildrenCount(VisualTreeHelper.GetChild(container, i)) > 0)
{
//recursively look at its children
foundControl = FindControlByType<T>(VisualTreeHelper.GetChild(container, i), name);
if (foundControl != null)
break;
}
}
return foundControl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment