Exemplo n.º 1
0
        protected override void OnKeyDown(KeyRoutedEventArgs e)
        {
            if (!Interaction.AllowKeyDown(e))
            {
                return;
            }

            base.OnKeyDown(e);

            if (e.Handled)
            {
                return;
            }

            // Some keys (e.g. Left/Right) need to be translated in RightToLeft mode
            VirtualKey invariantKey = InteractionHelper.GetLogicalKey(FlowDirection, e.Key);

            switch (invariantKey)
            {
            case VirtualKey.Left:
            {
                RatingItem ratingItem = FocusManager.GetFocusedElement() as RatingItem;

                if (ratingItem != null)
                {
                    ratingItem = GetRatingItemAtOffsetFrom(ratingItem, -1);
                }
                else
                {
                    ratingItem = GetRatingItems().FirstOrDefault();
                }
                if (ratingItem != null)
                {
                    if (ratingItem.Focus(FocusState.Keyboard))
                    {
                        e.Handled = true;
                    }
                }
            }
            break;

            case VirtualKey.Right:
            {
                RatingItem ratingItem = FocusManager.GetFocusedElement() as RatingItem;

                if (ratingItem != null)
                {
                    ratingItem = GetRatingItemAtOffsetFrom(ratingItem, 1);
                }
                else
                {
                    ratingItem = GetRatingItems().FirstOrDefault();
                }
                if (ratingItem != null)
                {
                    if (ratingItem.Focus(FocusState.Keyboard))
                    {
                        e.Handled = true;
                    }
                }
            }
            break;

            case VirtualKey.Add:
            {
                if (this.IsEnabled)
                {
                    RatingItem ratingItem = GetSelectedRatingItem();
                    if (ratingItem != null)
                    {
                        ratingItem = GetRatingItemAtOffsetFrom(ratingItem, 1);
                    }
                    else
                    {
                        ratingItem = GetRatingItems().FirstOrDefault();
                    }
                    if (ratingItem != null)
                    {
                        ratingItem.SelectValue();
                        e.Handled = true;
                    }
                }
            }
            break;

            case VirtualKey.Subtract:
            {
                if (this.IsEnabled)
                {
                    RatingItem ratingItem = GetSelectedRatingItem();
                    if (ratingItem != null)
                    {
                        ratingItem = GetRatingItemAtOffsetFrom(ratingItem, -1);
                    }
                    if (ratingItem != null)
                    {
                        ratingItem.SelectValue();
                        e.Handled = true;
                    }
                }
            }
            break;
            }
        }