Example #1
0
        void OnIsDropDownOpenChanged(bool newValue)
        {
            if (DropDown != null)
            {
                if (newValue)
                {
                    DropDown.PlacementTarget = this;
                    DropDown.Placement       = PlacementMode.Bottom;
                    DropDown.SetBinding(ContextMenu.IsOpenProperty, new Binding()
                    {
                        Source = this, Path = new PropertyPath(IsDropDownOpenProperty), Mode = BindingMode.TwoWay
                    });
                }
                else
                {
                    DropDown.ClearValue(ContextMenu.IsOpenProperty);
                    DropDown.ClearValue(ContextMenu.PlacementTargetProperty);
                    DropDown.ClearValue(ContextMenu.PlacementProperty);
                }
            }
            else
            {
                if (newValue)
                {
                    if (_contextMenu != null)
                    {
                        return;
                    }

                    _contextMenu = new ContextMenu();
                    _contextMenu.PlacementTarget = this;
                    _contextMenu.Placement       = PlacementMode.Bottom;
                    _contextMenu.SetBinding(ContextMenu.ItemsSourceProperty, new Binding()
                    {
                        Source = this, Path = new PropertyPath(DropDownItemsSourceProperty)
                    });
                    _contextMenu.SetBinding(ContextMenu.ItemContainerStyleProperty, new Binding()
                    {
                        Source = this, Path = new PropertyPath(DropDownItemStyleProperty)
                    });
                    _contextMenu.SetBinding(ContextMenu.IsOpenProperty, new Binding()
                    {
                        Source = this, Path = new PropertyPath(IsDropDownOpenProperty), Mode = BindingMode.TwoWay
                    });
                }
                else
                {
                    _contextMenu.ClearValue(ContextMenu.IsOpenProperty);
                    _contextMenu = null;
                }
            }
        }