在Infragistics XamDataCards中单击鼠标右键选择一张卡片

时间:2012-09-13 10:53:19

标签: .net wpf infragistics

如何在Infragistics XamDataCards WPF控件中单击鼠标右键选择卡? XamDataCards.ActiveDataItem属性也应该更改。

2 个答案:

答案 0 :(得分:1)

我已经有了解决方案:

/// <summary>
/// selects XamDataCards'es card on right button mouse down even
/// </summary>
public class XamDataCardsSelectCardOnRightMouseClickBehavior : Behavior<XamDataCards>
{
    protected override void OnAttached()
    {
        AssociatedObject.PreviewMouseRightButtonDown += AssociatedObjectPreviewMouseRightButtonDown;
    }

    /// <summary>
    /// select a card on right-click so that a context menu can be displayed
    /// </summary>
    /// <param name="sender">not used</param>
    /// <param name="e">contians mouse position</param>
    private void AssociatedObjectPreviewMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        VisualTreeHelper.HitTest(this.AssociatedObject, t => HitTestFilterBehavior.Continue, HitTestResultFunc, new PointHitTestParameters(e.GetPosition((UIElement)sender)));
    }

    /// <summary>
    /// WPF invokes this method to report hit test intersections to the user.
    /// If a card was hit, it is set as active (or selected) card.
    /// </summary>
    /// <param name="result">Contains the visual object that was hit.</param>
    /// <returns>Always returns Continue.</returns>
    public HitTestResultBehavior HitTestResultFunc(HitTestResult result)
    {
        DependencyObject dobj = result.VisualHit;
        if (VisualTreeHelper.GetParent(dobj).GetType() == typeof(Infragistics.Windows.DataPresenter.DataRecordPresenter))
        {
            DataRecordPresenter rp = (DataRecordPresenter) VisualTreeHelper.GetParent(dobj);
            this.AssociatedObject.ActiveDataItem = rp.DataRecord.DataItem;
        }
        return HitTestResultBehavior.Continue;
    }
}

然后只需将其附加到您的XamDataCards控件。

答案 1 :(得分:1)

如果右键单击了卡片的标题,此解决方案也会激活卡片:

class XamDataCardsActivateOnRightMouseClickBehavior : Behavior<XamDataCards>
{
    protected override void OnAttached()
    {
        AssociatedObject.PreviewMouseRightButtonDown += AssociatedObjectPreviewMouseRightButtonDown;
    }

    private void AssociatedObjectPreviewMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        CardViewCard card = Infragistics.Windows.Utilities.GetAncestorFromType(e.OriginalSource as DependencyObject, typeof(CardViewCard), false) as CardViewCard;
        if (card != null)
        {
            DataRecord record = card.DataContext as DataRecord;
            if (record != null)
            {
                record.DataPresenter.ActiveRecord = record;
            }
        }
    }
}