Git Product home page Git Product logo

wpf-autolayout's People

Contributors

trevorlinton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

wpf-autolayout's Issues

Suggestion: Use Expression<> rather than Strings for declaring constraints?

I was wondering if you would consider extending AddLayoutConstraint to support Expression<TDelegate> rather than using magic-strings for constraint declarations. This has numerous benefits, such as compile-time verification that a layout declaration is type-safe.

I haven't looked too closely at the source-code, but as you'd need to use reflection or WPF's DependencyProperty features then you'd need to use Expression<Func<...>> instead of a naked Func<...>.

An initial API would resemble the current AddLayoutConstraint method, just with generic arguments for type-safety, and using named overloads for =, <, and > instead of using magic-strings:

class AutoLayoutPanel
{
    // A better name might be "ConstrainEquality", perhaps?
    public Int32 AddEqualityConstraint<TLeft,TRight,TProperty>( 
        TLeft left,
        Expression<Func<TLeft ,TProperty>> leftProperty,
        TRight right,
        Expression<Func<TRight,TProperty>> rightProperty
    )
        where TLeft     : UIElement
        where TRight    : UIElement
        where TProperty : IComparable<TProperty>
    {
        // ...
    }

    public Int32 AddLessThanConstraint<TLeft,TRight,TProperty>( 
        TLeft left,
        Expression<Func<TLeft ,TProperty>> leftProperty,
        TRight right,
        Expression<Func<TRight,TProperty>> rightProperty
    )
        where TLeft     : UIElement
        where TRight    : UIElement
        where TProperty : IComparable<TProperty>
    {
        // ...
    }

    public Int32 AddGreaterThanConstraint<TLeft,TRight,TProperty>( 
        TLeft left,
        Expression<Func<TLeft ,TProperty>> leftProperty,
        TRight right,
        Expression<Func<TRight,TProperty>> rightProperty
    )
        where TLeft     : UIElement
        where TRight    : UIElement
        where TProperty : IComparable<TProperty>
    {
        // ...
    }
}

So the example call-sites from the README file would be rewritten as:

panel.AddEqualityConstraint( button, b => b.Left, panel, p => p.Left, multiplier: 1, constant: 0 );
panel.AddEqualityConstraint( button, b => b.Middle, panel, p => p.Middle, multiplier: 1, constant: 0 );

But these can be combined into a single AddLayoutConstraint method if it accepts a more general Expression argument to define the constraint:

    public Int32 AddLayoutConstraint<TLeft,TRight>( 
        TLeft left,
        TRight right,
        Expression<Func<TLeft,TRight,Boolean>> constraint
    )
        where TLeft  : UIElement
        where TRight : UIElement
    {
        // ...
    }

Thus allowing call-sites to be like this:

panel.AddLayoutConstraint( button, panel, ( b, p ) => b.Left == p.Right );

panel.AddLayoutConstraint( button, panel, ( b, p ) => b.Left * 2 + 3 == p.Right * 2 + 3 );

And this way - the constraints could be defined as C# code embedded in XAML that's evaluated when the control is initialized, thus avoiding the need for explicit AddLayoutConstraints entirely.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.