Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1435
XamDataGrid HeaderPrefixArea Style issue
posted

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>

  • 29105
    Verified Answer
    Offline posted

    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.

    1. Handle the Loaded event for the AssociatedObject the OnAttached method override in the Behavior
    2. Get a pointer to the XamDataGrid via the Infragistics Utilities class 
    3. Assign the pointer to ParentDG

    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