DataTrigger Style with Parent Control Type Reference

There are several GroupBox controls in my window, each of which contains Grid Control. For those grids I want to style. But only for those grids that are located directly in GroupBox, all other grids should not be affected.

I tried the following, which does not work, since GetType () is not a property.

<Style TargetType="Grid"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Parent.GetType()}" Value="{x:Type GroupBox}"> <!-- <Setter Property="..." Value="..."/> --> </DataTrigger> </Style.Triggers> </Style> 

I found a workaround, but this is not a very pretty solution, since I need to change GroupBoxes:

  <Style TargetType="GroupBox"> <Setter Property="Tag" Value="blub"/> </Style> <Style TargetType="Grid"> <Style.Triggers> <DataTrigger Binding="{Binding Path=Parent.Tag, RelativeSource={RelativeSource Mode=Self}}" Value="blub"> <!-- <Setter Property="..." Value="..."/> --> </DataTrigger> </Style.Triggers> </Style> 

Obviously, I could set the style for each Grid manually, but I try to avoid this, as there are so many of them. I hope you can find a way to do the first work example.

+6
styles wpf binding xaml
source share
3 answers
 <DataTrigger Binding="{Binding Path=Parent.Tag, RelativeSource={RelativeSource Mode=Self}}" Value="blub"> 

This code does not work because the mode type is actually a BindingMode, which is an enumeration, and none of its members is Self. So this assignment of Mode = Self is wrong in your code. Click here for possible mode values.

The right way to write this,

 <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Tag}" Value="blub"> 

And, of course, for this you need to keep this style for the GroupBox that you have already written.

+5
source share

This worked for me:

  <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StatusBar}}, Path=DependencyObjectType.Name}" Value="StatusBar"> <Setter Property="Margin" Value="0"/> <Setter Property="Padding" Value="0"/> <Setter Property="Background" Value="Chartreuse"/> </DataTrigger> </Style.Triggers> 

It allows you to set the style based on the parent type, without resorting to a method that should really be used by code, not markup.

+1
source share

Use the following code:

 using DevExpress.Xpf.Core.Native; using System; using System.Globalization; using System.Windows; using System.Windows.Data; namespace BindingErrorHelper { public class IsTypeFoundConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { FrameworkElement element = value as FrameworkElement; Type type = parameter as Type; if (element != null && type != null) { element = LayoutHelper.FindElement(element,type); if (element != null) return true; } return false; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return value; } } public class LayoutHelper { public static FrameworkElement FindElement(FrameworkElement treeRoot, Type type) { FrameworkElement parentElement = VisualTreeHelper.GetParent(treeRoot) as FrameworkElement; while (parentElement != null) { if (parentElement.GetType() == type) return parentElement; else parentElement = VisualTreeHelper.GetParent(parentElement) as FrameworkElement; } return null; } } } 

Enter the XAML code as:

 <tt:IsTypeFoundConverter x:Key="isTypeFoundConverter"/> <Style TargetType="Grid"> <Style.Triggers> <DataTrigger Binding={Binding RelativeSource={RelativeSource Self}, Converter={StaticResource isTypeFoundConverter}, ConverterParameter={x:Type GroupBox}}" Value="true"> <!-- <Setter Property="..." Value="..."/> --> </DataTrigger> </Style.Triggers> </Style> 
0
source share

All Articles