Пример #1
0
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (!Interaction.AllowKeyDown(e))
            {
                return;
            }

            base.OnKeyDown(e);

            if (e.Handled)
            {
                return;
            }

            switch (e.Key)
            {
            case Key.Left:
            {
#if SILVERLIGHT
                RatingItem ratingItem = FocusManager.GetFocusedElement() as RatingItem;
#else
                var ratingItem = FocusManager.GetFocusedElement(Application.Current.MainWindow) as RatingItem;
#endif
                if (ratingItem != null)
                {
                    ratingItem = GetRatingItemAtOffsetFrom(ratingItem, -1);
                }
                else
                {
                    ratingItem = GetRatingItems().FirstOrDefault();
                }
                if (ratingItem != null)
                {
                    if (ratingItem.Focus())
                    {
                        e.Handled = true;
                    }
                }
            }
            break;

            case Key.Right:
            {
#if SILVERLIGHT
                RatingItem ratingItem = FocusManager.GetFocusedElement() as RatingItem;
#else
                var ratingItem = FocusManager.GetFocusedElement(Application.Current.MainWindow) as RatingItem;
#endif
                if (ratingItem != null)
                {
                    ratingItem = GetRatingItemAtOffsetFrom(ratingItem, 1);
                }
                else
                {
                    ratingItem = GetRatingItems().FirstOrDefault();
                }
                if (ratingItem != null)
                {
                    if (ratingItem.Focus())
                    {
                        e.Handled = true;
                    }
                }
            }
            break;

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

            case Key.Subtract:
            {
                if (!IsReadOnly)
                {
                    RatingItem ratingItem = GetSelectedRatingItem();
                    if (ratingItem != null)
                    {
                        ratingItem = GetRatingItemAtOffsetFrom(ratingItem, -1);
                    }
                    if (ratingItem != null)
                    {
                        ratingItem.SelectValue();
                        e.Handled = true;
                    }
                }
            }
            break;
            }
        }
Пример #2
0
        protected override void OnKeyDown(KeyEventArgs 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
            Key invariantKey = InteractionHelper.GetLogicalKey(FlowDirection, e.Key);

            switch (invariantKey)
            {
            case Key.Left:
            {
#if SILVERLIGHT
                RatingItem ratingItem = FocusManager.GetFocusedElement() as RatingItem;
#else
                RatingItem ratingItem = FocusManager.GetFocusedElement(Application.Current.MainWindow) as RatingItem;
#endif
                if (ratingItem != null)
                {
                    ratingItem = GetRatingItemAtOffsetFrom(ratingItem, -1);
                }
                else
                {
                    ratingItem = GetRatingItems().FirstOrDefault();
                }
                if (ratingItem != null)
                {
                    if (ratingItem.Focus())
                    {
                        e.Handled = true;
                    }
                }
            }
            break;

            case Key.Right:
            {
#if SILVERLIGHT
                RatingItem ratingItem = FocusManager.GetFocusedElement() as RatingItem;
#else
                RatingItem ratingItem = FocusManager.GetFocusedElement(Application.Current.MainWindow) as RatingItem;
#endif
                if (ratingItem != null)
                {
                    ratingItem = GetRatingItemAtOffsetFrom(ratingItem, 1);
                }
                else
                {
                    ratingItem = GetRatingItems().FirstOrDefault();
                }
                if (ratingItem != null)
                {
                    if (ratingItem.Focus())
                    {
                        e.Handled = true;
                    }
                }
            }
            break;

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

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