0

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

到目前为止,我所拥有的是:

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

然而,转动轮子并没有改变背景图像。我怎样才能改变图像?

4

1 回答 1

1

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

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

(将类型替换为您的用户控件的类型)

于 2012-07-20T12:02:48.377 回答