将命令绑定到xctk的“LostMouseCapture”事件:IntegerUpDown

时间:2017-08-11 10:22:24

标签: wpf events mvvm data-binding command

我想将Icommand绑定到xctk:IntegerUpDown的事件。事件是'LostMouseCapture'

在XAML中:

<xctk:IntegerUpDown Name="MinColor" ClipValueToMinMax="True" Grid.Column="2" 
                                                    Minimum="0" Maximum="{Binding Path=MinColorMaxValue}" Value="{Binding Path=MinColorValue}" Increment="1"   
                                                    PreviewTextInput="IntegerUpDown_PreviewTextInput" DataObject.Pasting="IntegerUpDown_Pasting" PreviewKeyDown="IntegerUpDown_PreviewKeyDown" >
                                                    <i:Interaction.Triggers>
                                                        <i:EventTrigger EventName="LostMouseCapture">
                                                            <i:InvokeCommandAction Command="{Binding Path=ValueCommand}" CommandParameter="{Binding FileName}"/>
                                                        </i:EventTrigger>
                                                    </i:Interaction.Triggers>
                                                </xctk:IntegerUpDown> 

在ViewModel中:

private DelegateCommand m_ValueCommand = null;          

    public ICommand ValueCommand
    {
        get
        {
            if (m_ValueCommand == null)
                m_ValueCommand = new DelegateCommand(new Action<object>(ExecuteValueCommand),
                    new Predicate<object>(CanExecuteValueCommand));
            return m_ValueCommand;
        }
    }

    public bool CanExecuteValueCommand(object sender)
    {  
        return true;
    }

    public void ExecuteValueCommand(object sender)
    {
    }

当我在IntegerUpdown中输入一些值并点击外部时,我想调用'ValueCommand'。对此有什么解决方案吗?

1 个答案:

答案 0 :(得分:1)

MinColorValue源属性的设置者中调用您的命令:

private int _minColorValue;
public int MinColorValue
{
    get { return _minColorValue; }
    set
    {
        _minColorValue = value;
        ValueCommand.Execute(FileName);
    }
}

或尝试处理LostKeyboardFocusLostFocus事件:

<i:EventTrigger EventName="LostKeyboardFocus">