Search code examples
wpfresizecontrolstraversal

drag items in resizable canvas


I'm designing a spline editor which is composed of two parts (usercontrols) The left control is the DesignerControl and the right one is the InfoControl. They share the same DataContext: DesignerVM with an

ObservableCollection<SplineVM> SplineVMList;

DesignerControl is an ItemsControl templated as a Canvas ("mycanvas") with ItemsSource bound to SplineVMList and ItemTemplate set as SplineControl.
InfoControl is a ListBox displaying the SplineVMs and a Grid displaying the SplineVMs' Properties.

In SplineControl I have a Canvas containing 4 draggable Points (Rectangle) and 2 Lines that will be bound to these Points. Everything works, I can drag my points, my lines move.

<UserControl>
    <Canvas Width="300" Height="300" x:Name="mycanvas" Background="Transparent">
        <Path x:Name="curve" Stroke="#F02828" StrokeThickness="3">
            <Path.Data>
                <PathGeometry>
                    <PathGeometry.Figures>
                        <PathFigureCollection>
                            <PathFigure>
                                <PathFigure.Segments>
                                    <PathSegmentCollection x:Name="pathsegment"/>
                                </PathFigure.Segments>
                            </PathFigure>
                        </PathFigureCollection>
                    </PathGeometry.Figures>
                </PathGeometry>
            </Path.Data>
        </Path>
        <Rectangle x:Name="curvePoint1" Width="10" Height="10" Canvas.Bottom="0" Canvas.Left="0" />
        <Rectangle x:Name="curvePoint2" Width="10" Height="10" RadiusX="4" RadiusY="4" Canvas.Bottom="0" Canvas.Left="{Binding ElementName=mycanvas, Path=ActualWidth, Converter={StaticResource mathconverter}, ConverterParameter=(@VALUE/4)}"/>
        <Rectangle x:Name="curvePoint3" Width="10" Height="10" RadiusX="4" RadiusY="4" Canvas.Bottom="0" Canvas.Left="{Binding ElementName=mycanvas, Path=ActualWidth, Converter={StaticResource mathconverter}, ConverterParameter=(@VALUE/2)}"/>
        <Rectangle x:Name="curvepoint4" Width="10" Height="10" Canvas.Bottom="0" Canvas.Left="{Binding ElementName=mycanvas, Path=ActualWidth, Converter={StaticResource mathconverter}, ConverterParameter=(@VALUE)}"/>
        <Line x:Name="line1" Stroke="#dfdfdf" StrokeThickness="1"/>
        <Line x:Name="line2" Stroke="#dfdfdf" StrokeThickness="1"/>
    </Canvas>
</UserControl>

My first problem is that I have to use a container (here a Canvas) to hold the path, rectangles and lines.

When i add a SplineControl in mycanvas, it's well placed and i can instantly drag my points but when i add another UserControl, it's placed above the previous one and i can't select the first Usercontrol's points.

I don't want to use IsHitTestVisible since I want to select points from the first userControl through the second.

My Second problem: Since I use a Canvas to hold my things in the SplineControl, i can drag points outside of the canvas and still interact with it, and at first sight it was great.

But thinking it again, i'd like to resize my Canvas when i move a point to always have my point in the canvas. but also have my other points positions updated respecting the ratio of mycanvas.

Do you know any control that have this behavior and can replace my Canvas? Should i use CustomControl instead of UserControl?

May I have to think again my project's conception?


Solution

  • In Fact I've found a way to get around my first problem of item selection through other items.

    Canvas that were under the selected one were unclickable because the Color of every Canvas was set to Transparent. So I could see them but not interact with them.

    Since I've set the Background property to Null (or haven't set it explicitly), I can click through and select the items under the top one.

    It's a strange behavior of WPF... Does someone have an explanation?

    It will solve my 2nd problem too, 'cause I'll set my UserControl with the size I want and still select other UserControls in my View.

    It's a dirty trick but I'll avoid the CustomPanel thing then.

    thx anyway akjoshi and Danny Varod for your concern ^^ your answers were still useful since I'll use them in the other project I'm working on. it's just that time is short on this one.