summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Controls/DropButton/DropButton.cs
blob: 22383b871a779fd970197db06d4b1a7c4edd46f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DropButton.cs" company="HandBrake Project (http://handbrake.fr)">
//   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
//   Defines the DropDownButton type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace HandBrakeWPF.Controls.DropButton
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Data;

    /// <summary>
    /// The drop down button.
    /// </summary>
    public class DropButton : ToggleButton
    {
        /// <summary>
        /// The drop down property.
        /// </summary>
        public static readonly DependencyProperty DropDownProperty =
          DependencyProperty.Register("DropDown",
                                      typeof(ContextMenu),
                                      typeof(DropButton),
                                      new UIPropertyMetadata(null));

        /// <summary>
        /// Initializes a new instance of the <see cref="DropButton"/> class.
        /// </summary>
        public DropButton()
        {
            // Bind the ToogleButton.IsChecked property to the drop-down's IsOpen property 
            Binding binding = new Binding("DropDown.IsOpen") { Source = this };
            this.SetBinding(IsCheckedProperty, binding);
        }

        /// <summary>
        /// Gets or sets the drop down.
        /// </summary>
        public ContextMenu DropDown
        {
            get { return (ContextMenu)this.GetValue(DropDownProperty); }
            set { this.SetValue(DropDownProperty, value); }
        }

        /// <summary>
        /// Handle the users click on the button.
        /// </summary>
        protected override void OnClick()
        {
            if (this.DropDown != null)
            {
                // If there is a drop-down assigned to this button, then position and display it 

                this.DropDown.PlacementTarget = this;
                this.DropDown.Placement = PlacementMode.Bottom;
                this.DropDown.IsOpen = true;
            }
        }
    }
}