Hi,
I have a style that inserts a Button in the HeaderPrefixArea and a behavior (with a Dependency Property) that I'm attaching to the Button.
I'm binding the Dependency Property to the XamDataGrid. The issue is when I use the RelativeSource binding, the behavoir is throwing an exception. If I bind by ElementName it works.
The reason I want to use the RelativeSource is because I want this style to be in the App.xaml
Please see attached sample.
<igDP:XamDataGrid.Resources> <Style TargetType="{x:Type igDP:HeaderPrefixArea}" BasedOn="{x:Null}"> <Setter Property="Visibility" Value="Visible" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:HeaderPrefixArea}"> <StackPanel Orientation="Horizontal" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}"> <Button Name="testButton" VerticalAlignment="Center" Height="25" Content="Click Me"> <i:Interaction.Behaviors> <local:ButtonBehavior ParentDG="{Binding ElementName=dataGrid}" /> <!--<local:ButtonBehavior ParentDG="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type igDP:XamDataGrid}}}" />--> </i:Interaction.Behaviors> </Button> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </igDP:XamDataGrid.Resources>
Your welcome! Glad I could help.
- Michael
Thank you Michael. Great answer !
Hello User,
Thank you for contacting Infragistics. I've reproduced the issue and the behavior is to be expected. RelativeSource will not work for dependency properties for Behaviors through XAML since Behaviors are not visual elements and do not have the concept of an Ancestor/Descendent, thus do not reside in the Visual Tree.
To work around this you can assign the XamDataGrid through your Behavior, in code.
protected override void OnAttached() { base.OnAttached(); AssociatedObject.Loaded += AssociatedObject_Loaded; AssociatedButton.MouseLeftButtonUp += AssociatedButtonMouseLeftButtonUp; } private void AssociatedObject_Loaded(object sender, RoutedEventArgs e) { var t = Utilities.GetAncestorFromType(AssociatedObject as DependencyObject, typeof(XamDataGrid), false); if (t != null) { this.ParentDG = (XamDataGrid)t; ParentDG.RecordFilterChanged += ParentDGRecordFilterChanged; } }
I modified the sample and reattached it below. Let me know if you have any questions.
6646.Demo.zip