protected override FrameworkElement CreateControlCore() { FrameworkElement generatedResult = null; var groupName = this.CommandMetaGroup.Name.Translate(); //尝试查找已经生成的该组的按钮 var splitButton = this.Context.ContainerItems.OfType<SplitButton>() .FirstOrDefault(btnGroup => btnGroup.Content.Equals(groupName)); //还没有生成,则创建一个新的。 if (splitButton == null) { splitButton = new SplitButton() { Mode = SplitButtonMode.Dropdown, Margin = new Thickness(2), Content = groupName, }; //只返回刚生成的 SplitButton generatedResult = splitButton; } this.GenerateButtons(splitButton.Items); return generatedResult; }
/* * DependencyPropertyChanged callbacks * */ private static void OnIsContextMenuOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { SplitButton s = (SplitButton)d; s.EnsureContextMenuIsValid(); if (!s.ContextMenu.HasItems) { return; } bool value = (bool)e.NewValue; if (value && !s.ContextMenu.IsOpen) { s.ContextMenu.IsOpen = true; } else if (!value && s.ContextMenu.IsOpen) { s.ContextMenu.IsOpen = false; } }