Skip to content

Conversation

@gerritvanroekel
Copy link

When using a scroll viewer on a custom control the given ContentPresenter isn't correctly returned by the GetVisualAncestor() method. Because there are 2 (the scrollcontentpresenter and the own contentpresenter). Therefore the Collection can't be determinate, and a null reference exception will be thrown.

Example Control

    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Background="Red">
                        <ScrollViewer>
                            <ItemsPresenter />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

and the class file


public class CustomControl1 : ItemsControl
    {

        public IDragSource DragHandler
        {
            get { return (IDragSource)GetValue(GongSolutions.Wpf.DragDrop.DragDrop.DragHandlerProperty); }
            set { SetValue(GongSolutions.Wpf.DragDrop.DragDrop.DragHandlerProperty, value); }
        }

        public IDropTarget DropHandler
        {
            get { return (IDropTarget)GetValue(GongSolutions.Wpf.DragDrop.DragDrop.DropHandlerProperty); }
            set { SetValue(GongSolutions.Wpf.DragDrop.DragDrop.DropHandlerProperty, value); }
        }

        static CustomControl1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
        }

        public CustomControl1()
        {
            SetValue(GongSolutions.Wpf.DragDrop.DragDrop.IsDragSourceProperty, true);
            DragHandler = new DragSource();
            SetValue(GongSolutions.Wpf.DragDrop.DragDrop.IsDropTargetProperty, true);
            DropHandler = new MoveDropTarget();
        }

    }

And in the MainWindow.xaml

<l:CustomControl1 ItemsSource="{Binding MyProperty}"  Grid.Column="0">
                    <l:CustomControl1.ItemTemplate>
                        <DataTemplate>
                            <Border Background="Yellow" Margin="4">
                                <TextBlock Text="{Binding Id}" />
                            </Border>
                        </DataTemplate>
                    </l:CustomControl1.ItemTemplate>
                </l:CustomControl1>

punker76 added a commit that referenced this pull request Mar 26, 2015
Fix an issue when the itempresenter contains more ContentControls
@punker76 punker76 merged commit 9e85a9e into punker76:master Mar 26, 2015
@punker76
Copy link
Owner

@gerritvanroekel thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants