| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- // (c) Copyright Microsoft Corporation.
- // This source is subject to the Microsoft Public License (Ms-PL).
- // Please see http://go.microsoft.com/fwlink/?LinkID=131993] for details.
- // All other rights reserved.
- using System.Diagnostics.CodeAnalysis;
- using System.Globalization;
- using System.Windows.Automation.Peers;
- using System.Windows.Controls.Primitives;
- using System.Windows.Input;
- namespace System.Windows.Controls
- {
- /// <summary>
- /// An item used in a rating control.
- /// </summary>
- /// <QualityBand>Preview</QualityBand>
- [TemplateVisualState(Name = VisualStates.StateNormal, GroupName = VisualStates.GroupCommon)]
- [TemplateVisualState(Name = VisualStates.StateMouseOver, GroupName = VisualStates.GroupCommon)]
- [TemplateVisualState(Name = VisualStates.StatePressed, GroupName = VisualStates.GroupCommon)]
- [TemplateVisualState(Name = VisualStates.StateDisabled, GroupName = VisualStates.GroupCommon)]
- [TemplateVisualState(Name = VisualStates.StateReadOnly, GroupName = VisualStates.GroupCommon)]
- [TemplateVisualState(Name = VisualStates.StateFocused, GroupName = VisualStates.GroupFocus)]
- [TemplateVisualState(Name = VisualStates.StateUnfocused, GroupName = VisualStates.GroupFocus)]
- [TemplateVisualState(Name = StateFilled, GroupName = GroupFill)]
- [TemplateVisualState(Name = StateEmpty, GroupName = GroupFill)]
- [TemplateVisualState(Name = StatePartial, GroupName = GroupFill)]
- public class RatingItem : ButtonBase, IUpdateVisualState
- {
- /// <summary>
- /// The state in which the item is filled.
- /// </summary>
- private const string StateFilled = "Filled";
- /// <summary>
- /// The state in which the item is empty.
- /// </summary>
- private const string StateEmpty = "Empty";
- /// <summary>
- /// The group that contains fill states.
- /// </summary>
- private const string GroupFill = "FillStates";
- /// <summary>
- /// The state in which the item is partially filled.
- /// </summary>
- private const string StatePartial = "Partial";
- /// <summary>
- /// The interaction helper used to get the common states working.
- /// </summary>
- private InteractionHelper _interactionHelper;
- #region public double DisplayValue
- /// <summary>
- /// A value indicating whether the actual value is being set.
- /// </summary>
- private bool _settingDisplayValue;
- /// <summary>
- /// Gets the actual value.
- /// </summary>
- public double DisplayValue
- {
- get { return (double)GetValue(DisplayValueProperty); }
- internal set
- {
- _settingDisplayValue = true;
- try
- {
- SetValue(DisplayValueProperty, value);
- }
- finally
- {
- _settingDisplayValue = false;
- }
- }
- }
- /// <summary>
- /// Identifies the DisplayValue dependency property.
- /// </summary>
- public static readonly DependencyProperty DisplayValueProperty =
- DependencyProperty.Register(
- "DisplayValue",
- typeof(double),
- typeof(RatingItem),
- new PropertyMetadata(0.0, OnDisplayValueChanged));
- /// <summary>
- /// DisplayValueProperty property changed handler.
- /// </summary>
- /// <param name="d">RatingItem that changed its DisplayValue.</param>
- /// <param name="e">Event arguments.</param>
- private static void OnDisplayValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- RatingItem source = (RatingItem)d;
- source.OnDisplayValueChanged((double)e.OldValue, (double)e.NewValue);
- }
- /// <summary>
- /// DisplayValueProperty property changed handler.
- /// </summary>
- /// <param name="oldValue">The old value.</param>
- /// <param name="newValue">The new value.</param>
- private void OnDisplayValueChanged(double oldValue, double newValue)
- {
- if (!_settingDisplayValue)
- {
- _settingDisplayValue = true;
- this.DisplayValue = oldValue;
- throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, Muchinfo.WPF.Controls.Properties.Resources.InvalidAttemptToChangeReadOnlyProperty, "DisplayValue"));
- }
- else
- {
- if (newValue <= 0.0)
- {
- VisualStates.GoToState(this, true, StateEmpty);
- }
- else if (newValue >= 1.0)
- {
- VisualStates.GoToState(this, true, StateFilled);
- }
- else
- {
- VisualStates.GoToState(this, true, StatePartial);
- }
- }
- }
- #endregion public double DisplayValue
- #region public bool IsReadOnly
- /// <summary>
- /// A value indicating whether the read only value is being set.
- /// </summary>
- private bool _settingIsReadOnly;
- /// <summary>
- /// Gets a value indicating whether the control is read-only.
- /// </summary>
- public bool IsReadOnly
- {
- get { return (bool)GetValue(IsReadOnlyProperty); }
- internal set
- {
- _settingIsReadOnly = true;
- try
- {
- SetValue(IsReadOnlyProperty, value);
- }
- finally
- {
- _settingIsReadOnly = false;
- }
- }
- }
- /// <summary>
- /// Identifies the IsReadOnly dependency property.
- /// </summary>
- public static readonly DependencyProperty IsReadOnlyProperty =
- DependencyProperty.Register(
- "IsReadOnly",
- typeof(bool),
- typeof(RatingItem),
- new PropertyMetadata(false, OnIsReadOnlyChanged));
- /// <summary>
- /// IsReadOnlyProperty property changed handler.
- /// </summary>
- /// <param name="d">RatingItem that changed its IsReadOnly.</param>
- /// <param name="e">Event arguments.</param>
- private static void OnIsReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- RatingItem source = (RatingItem)d;
- bool oldValue = (bool)e.OldValue;
- bool newValue = (bool)e.NewValue;
- source.OnIsReadOnlyChanged(oldValue, newValue);
- }
- /// <summary>
- /// IsReadOnlyProperty property changed handler.
- /// </summary>
- /// <param name="oldValue">Old value.</param>
- /// <param name="newValue">New value.</param>
- protected virtual void OnIsReadOnlyChanged(bool oldValue, bool newValue)
- {
- if (!_settingIsReadOnly)
- {
- _settingIsReadOnly = true;
- this.IsReadOnly = oldValue;
- throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, Muchinfo.WPF.Controls.Properties.Resources.InvalidAttemptToChangeReadOnlyProperty, "IsReadOnly"));
- }
- else
- {
- _interactionHelper.OnIsReadOnlyChanged(newValue);
- }
- }
- #endregion public bool IsReadOnly
- /// <summary>
- /// Gets or sets the parent rating of this rating item.
- /// </summary>
- internal Rating ParentRating { get; set; }
- #region public double Value
- /// <summary>
- /// Gets or sets the value property.
- /// </summary>
- [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "Value is the logical name for this property.")]
- internal double Value
- {
- get { return (double)GetValue(ValueProperty); }
- set { SetValue(ValueProperty, value); }
- }
- /// <summary>
- /// Identifies the Value dependency property.
- /// </summary>
- internal static readonly DependencyProperty ValueProperty =
- DependencyProperty.Register(
- "Value",
- typeof(double),
- typeof(RatingItem),
- new PropertyMetadata(0.0));
- /// <summary>
- /// Selects a value and raises the value selected event.
- /// </summary>
- internal void SelectValue()
- {
- if (!this.IsReadOnly)
- {
- this.Value = 1.0;
- OnClick();
- }
- }
- #endregion public double Value
- #if !SILVERLIGHT
- /// <summary>
- /// Initializes the static members of the ColumnDataPoint class.
- /// </summary>
- static RatingItem()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(RatingItem), new FrameworkPropertyMetadata(typeof(RatingItem)));
- }
- #endif
- /// <summary>
- /// Initializes a new instance of the RatingItem class.
- /// </summary>
- public RatingItem()
- {
- #if SILVERLIGHT
- this.DefaultStyleKey = typeof(RatingItem);
- #endif
- _interactionHelper = new InteractionHelper(this);
- }
- /// <summary>
- /// Provides handling for the RatingItem's MouseLeftButtonDown event.
- /// </summary>
- /// <param name="e">Event arguments.</param>
- protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
- {
- if (_interactionHelper.AllowMouseLeftButtonDown(e))
- {
- _interactionHelper.OnMouseLeftButtonDownBase();
- }
- base.OnMouseLeftButtonDown(e);
- }
- /// <summary>
- /// Provides handling for the RatingItem's MouseLeftButtonUp event.
- /// </summary>
- /// <param name="e">Event arguments.</param>
- protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
- {
- if (_interactionHelper.AllowMouseLeftButtonUp(e))
- {
- _interactionHelper.OnMouseLeftButtonUpBase();
- }
- base.OnMouseLeftButtonUp(e);
- }
- /// <summary>
- /// This method is invoked when the mouse enters the rating item.
- /// </summary>
- /// <param name="e">Information about the event.</param>
- protected override void OnMouseEnter(MouseEventArgs e)
- {
- if (_interactionHelper.AllowMouseEnter(e))
- {
- _interactionHelper.UpdateVisualStateBase(true);
- }
- base.OnMouseEnter(e);
- }
- /// <summary>
- /// This method is invoked when the mouse leaves the rating item.
- /// </summary>
- /// <param name="e">Information about the event.</param>
- protected override void OnMouseLeave(MouseEventArgs e)
- {
- if (_interactionHelper.AllowMouseLeave(e))
- {
- _interactionHelper.UpdateVisualStateBase(true);
- }
- base.OnMouseLeave(e);
- }
- /// <summary>
- /// Sets the value to 1.0 when clicked.
- /// </summary>
- protected override void OnClick()
- {
- base.OnClick();
- }
- /// <summary>
- /// Updates the visual state.
- /// </summary>
- /// <param name="useTransitions">A value indicating whether to use
- /// transitions.</param>
- void IUpdateVisualState.UpdateVisualState(bool useTransitions)
- {
- _interactionHelper.UpdateVisualStateBase(useTransitions);
- }
- /// <summary>
- /// Returns a AccordionItemAutomationPeer for use by the Silverlight
- /// automation infrastructure.
- /// </summary>
- /// <returns>A AccordionItemAutomationPeer object for the AccordionItem.</returns>
- protected override AutomationPeer OnCreateAutomationPeer()
- {
- return new RatingItemAutomationPeer(this);
- }
- }
- }
|