Esempio n. 1
0
        // Set the icon to the command text if no icon has been explicitly specified
        private static object CoerceSource(DependencyObject d, object value)
        {
            CommandMenuItem menuItem = (CommandMenuItem)d;

            // If no icon has been set, use the command's text
            if (menuItem.IsValueUnsetAndNull(SourceProperty, value) &&
                menuItem.Command is IRelayInfoCommand uiCommand)
            {
                value = uiCommand.Icon;
            }

            return(value);
        }
Esempio n. 2
0
        // Set the header to the command text if no header has been explicitly specified
        private static object CoerceHeader(DependencyObject d, object value)
        {
            CommandMenuItem   menuItem = (CommandMenuItem)d;
            IRelayInfoCommand uiCommand;

            // If no header has been set, use the command's text
            bool unset = menuItem.IsValueUnsetAndNull(HeaderProperty, value);

            if (menuItem.IsValueUnsetAndNull(HeaderProperty, value))
            {
                uiCommand = menuItem.Command as IRelayInfoCommand;
                if (uiCommand != null)
                {
                    value = uiCommand.Text;
                }
                return(value);
            }

            // If the header had been set to a UICommand by the ItemsControl, replace it with the command's text
            uiCommand = value as IRelayInfoCommand;

            if (uiCommand != null)
            {
                // The header is equal to the command.
                // If this MenuItem was generated for the command, then go ahead and overwrite the header
                // since the generator automatically set the header.
                ItemsControl parent = ItemsControl.ItemsControlFromItemContainer(menuItem);
                if (parent != null)
                {
                    object originalItem = parent.ItemContainerGenerator.ItemFromContainer(menuItem);
                    if (originalItem == value)
                    {
                        return(uiCommand.Text);
                    }
                }
            }

            return(value);
        }