0

I am creating a UserControl and one behaviour I want it to have is that when the user rotates the mouse wheel over it then the background image alternates between two options.

What I have so far is:

<UserControl x:Class="OI.MR.UserControls.DataControls.ScrollWheel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="400" d:DesignWidth="118">
    <UserControl.Background>
        <ImageBrush ImageSource="dial1.png" TileMode="None" />
    </UserControl.Background>
    <UserControl.InputBindings>
        <MouseBinding MouseAction="WheelClick" Command="{Binding ScrollTheWheel}"/>
    </UserControl.InputBindings>
</UserControl>

and

public partial class ScrollWheel : UserControl
{
    private bool _isDial1 = true;

    public ScrollWheel()
    {
        InitializeComponent();
    }

    private ICommand _scrollTheWheel;
    public ICommand ScrollTheWheel
    {
        get
        {
            if(_scrollTheWheel == null)
            {
                _scrollTheWheel = new DelegateCommand(_ => SwitchImage(), _ => true);
            }
            return _scrollTheWheel;
        }
    }

    private void SwitchImage()
    {
        if(_isDial1)
        {
            (Background as ImageBrush).ImageSource = new BitmapImage(new Uri("dial2.png"));
            _isDial1 = false;
        }
        else
        {
            (Background as ImageBrush).ImageSource = new BitmapImage(new Uri("dial1.png"));
            _isDial1 = true;   
        }
    }
}

However turning the wheel is not changing the background image. How can I get the image to change?

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
  • 1
    did you try clicking the wheel? --> thats the event you registered on: MouseAction="WheelClick" – fixagon Jul 20 '12 at 11:39
  • Your action is `WheelClick` not Scroll. – ChrisF Jul 20 '12 at 11:39
  • @ChrisF: ["WheelClick: A mouse wheel rotation. "](http://msdn.microsoft.com/en-us/library/system.windows.input.mouseaction.aspx) If MSDN is wrong, I'd love to know the right action I should use. – Matt Ellen Jul 20 '12 at 11:47
  • @MattEllen - I stand corrected. Have you verified that it's getting into `SwitchImage` then? – ChrisF Jul 20 '12 at 11:48
  • @fix_likes_coding clicking the wheel doesn't make it work, sadly. – Matt Ellen Jul 20 '12 at 11:50
  • is it possible that your command will never be bound as you override the DataContext of the UserControl? – fixagon Jul 20 '12 at 11:53
  • @ChrisF - it doesn't look like the method gets called. – Matt Ellen Jul 20 '12 at 11:55
  • @fix_likes_coding - maybe. I don't change the DataContext as far as I can tell. Maybe I need to set the DataContext? Does it need to be set to the control itself? – Matt Ellen Jul 20 '12 at 11:56
  • 1
    It doesn't look you aren't binding the `DataContext` of the control at all. This means that the subsequent bindings won't work. – ChrisF Jul 20 '12 at 11:57
  • @ChrisF - yes that's it. Now I have to find that question about binding to a control's own properties... – Matt Ellen Jul 20 '12 at 12:02

1 Answers1

1

your datacontext is really not correct: One possibility:

<UserControl.InputBindings>
    <MouseBinding MouseAction="WheelClick" 
        Command="{Binding Path=ScrollTheWheel, RelativeSource={RelativeSource AncestorType={x:Type view:YourUserControl}}}"/>
</UserControl.InputBindings>

(replace the type with the type of your user control)

fixagon
  • 5,506
  • 22
  • 26