如何使鼠标滚轮更改背景图像 [英] How to get mouse wheel to change the background image

查看:169
本文介绍了如何使鼠标滚轮更改背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个UserControl,我希望它具有的一种行为是,当用户在其上旋转鼠标滚轮时,背景图像会在两个选项之间交替显示.

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.

到目前为止,我有:

<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>

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?

推荐答案

您的数据上下文确实不正确: 一种可能性:

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)

这篇关于如何使鼠标滚轮更改背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆