Tuesday, 28 August 2012

WPF ScrollViewer Drag and Scroll Behavior

Here i am trying to add behavior to my ScrollViewer in WPF, where when i am dragging anything to ItemsControl and list is longer than User should be able  to drag that item to end of the list and list should scroll automaticall.

Following code will include drag/scroll behavior in ScrollViewer!

public class DragBehavior : Behavior
    {
        const double Tolerance = 100;
        const double Offset = 10;

        protected override void OnAttached()
        {
            var svFavourites = AssociatedObject;
            AssociatedObject.DragOver += (sender, e) =>
                {
                    var verticalPos = e.GetPosition(svFavourites).Y;

                    if (verticalPos < Tolerance)
                    {
                        svFavourites.ScrollToVerticalOffset(svFavourites.VerticalOffset - Offset);
                    }
                    else if (verticalPos > svFavourites.ActualHeight - Tolerance)
                    {
                        svFavourites.ScrollToVerticalOffset(svFavourites.VerticalOffset + Offset);
                    }
                };
        }
    }


To use the above Behavior just add following inside ScrollViewer in XAML


                   
               



Wednesday, 18 July 2012

Previous method "method name" requires a return value or an exception to throw

Today i came across this error, i took me a while to figure out the solution. following is an example


[TestFixture]
    public class RhinoTest
    {
        public interface ICompany
        {
            IPerson Contact { get; }
        }

        public interface IPerson
        {
            string Name { get; }
            string Address { get; }
        }

        [Test]
        public void RhinoMockCanStubMultipleLevels()
        {
            var company = MockRepository.GenerateMock();
            company.Stub(x => x.Contact.Name).Return("contact name");
            company.Stub(x => x.Contact.Address).Return("address");
            Assert.That(company.Contact.Name, Is.EqualTo("contact name"));
        }       
    }

 Above gave me error "Previous method "method name" requires a return value or an exception to throw" on Red line. Following was my solution, i had to Mock Contact separately.

[TestFixture]
    public class RhinoTest
    {
        public interface ICompany
        {
            IPerson Contact { get; }
        }

        public interface IPerson
        {
            string Name { get; }
            string Address { get; }
        }

        [Test]
        public void RhinoMockCanStubMultipleLevels()
        {
            var company = MockRepository.GenerateMock();
            var contact = MockRepository.GenerateMock();
            contact.Stub(x => x.Name).Return("contact name");
            contact.Stub(x => x.Address).Return("address");
            Assert.That(company.Contact.Name, Is.EqualTo("contact name"));
        }       
    }