WPF RibbonControlsLibrary RibbonSplitButton 项目问题

发布于 2024-11-02 07:07:10 字数 2585 浏览 2 评论 0原文

我正在使用 RibbonSplitButton 及其下拉菜单中的菜单项来模仿 Visual Studio 的撤消重做按钮。 我们有撤消重做堆栈,我有一个 dependencypropertychanged 事件处理程序,它将根据堆栈更新 UI。问题是,分割按钮的 items 属性正在使用 Collection,即使其项目集合的顺序正确,它也不会显示它们,因为它们是按索引排序的。

我将在下面提供一些示例来更好地解释这一点:

代码:

private static void UndoRedoUpdated(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        VO3Main main = (VO3Main)Application.Current.MainWindow;
        MenuItem item;
        int dif;

        if (main.UndoCommands != null)
        {
            dif = main.UndoCommands.Count - main._undoMenu.Items.Count;
            if (dif > 0)
            {
                for (int i = dif - 1; i >= 0; i--)
                {
                    item = new MenuItem();
                    item.Header = main.UndoCommands[i].Name;
                    item.Click += new RoutedEventHandler(main.undoMenu_Click);
                    main._undoMenu.Items.Insert(0, item);
                }
            }
            else if (dif < 0)
            {
                for (int i = 0; i < -dif; i++)
                    main._undoMenu.Items.RemoveAt(0);
            }
        }

        if (main.RedoCommands != null)
        {
            dif = main.RedoCommands.Count - main._redoMenu.Items.Count;

            if (dif > 0)
            {
                for (int i = dif - 1; i >= 0; i--)
                {
                    item = new MenuItem();
                    item.Header = main.RedoCommands[i].Name;
                    item.Click += new RoutedEventHandler(main.redoMenu_Click);
                    main._redoMenu.Items.Insert(0, item);
                }
            }
            else if (dif < 0)
            {
                for (int i = 0; i < -dif; i++)
                    main._redoMenu.Items.RemoveAt(0);
            }
        }
    }

XAML:

<r:RibbonGroup GroupSizeDefinitions="{StaticResource RibbonLayoutSmall}">
       <r:RibbonGroup.Command>
              <r:RibbonCommand LabelTitle="Editing"/>
       </r:RibbonGroup.Command>
       <r:RibbonSplitButton Name="_undoMenu" Command="me:AppCommands.Undo" MaxHeight="50"/>
       <r:RibbonSplitButton Name="_redoMenu" Command="me:AppCommands.Redo" MaxHeight="50"/>
</r:RibbonGroup>

实际撤消菜单项

Undo Menu Items in Code

PS 即使我将 0 处的插入更改为 Add,因此它将添加到集合的最后一个而不是第一个,它似乎没有什么区别... 如果有人能给我一些有关正在发生的事情以及如何解决此问题的信息,我将不胜感激。 提前致谢。

I am using a RibbonSplitButton to with menuitems in its dropdown to mimic visual studio's undo redo button.
We have the undo redo stacks and I have a dependencypropertychanged event handler that will update the UI based on the stacks. The problem is, the splitbutton's items property is using a Collection, and even though its collection of items are in the right order, it won't display them as they are ordered by index.

I will provide some examples below to explain this better:

Code:

private static void UndoRedoUpdated(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        VO3Main main = (VO3Main)Application.Current.MainWindow;
        MenuItem item;
        int dif;

        if (main.UndoCommands != null)
        {
            dif = main.UndoCommands.Count - main._undoMenu.Items.Count;
            if (dif > 0)
            {
                for (int i = dif - 1; i >= 0; i--)
                {
                    item = new MenuItem();
                    item.Header = main.UndoCommands[i].Name;
                    item.Click += new RoutedEventHandler(main.undoMenu_Click);
                    main._undoMenu.Items.Insert(0, item);
                }
            }
            else if (dif < 0)
            {
                for (int i = 0; i < -dif; i++)
                    main._undoMenu.Items.RemoveAt(0);
            }
        }

        if (main.RedoCommands != null)
        {
            dif = main.RedoCommands.Count - main._redoMenu.Items.Count;

            if (dif > 0)
            {
                for (int i = dif - 1; i >= 0; i--)
                {
                    item = new MenuItem();
                    item.Header = main.RedoCommands[i].Name;
                    item.Click += new RoutedEventHandler(main.redoMenu_Click);
                    main._redoMenu.Items.Insert(0, item);
                }
            }
            else if (dif < 0)
            {
                for (int i = 0; i < -dif; i++)
                    main._redoMenu.Items.RemoveAt(0);
            }
        }
    }

XAML:

<r:RibbonGroup GroupSizeDefinitions="{StaticResource RibbonLayoutSmall}">
       <r:RibbonGroup.Command>
              <r:RibbonCommand LabelTitle="Editing"/>
       </r:RibbonGroup.Command>
       <r:RibbonSplitButton Name="_undoMenu" Command="me:AppCommands.Undo" MaxHeight="50"/>
       <r:RibbonSplitButton Name="_redoMenu" Command="me:AppCommands.Redo" MaxHeight="50"/>
</r:RibbonGroup>

Actual Undo Menu Items

Undo Menu Items in Code

P.S. even if I change the insert at 0 to a Add, so it will add to the last of the collection instead of first, it doesn't seem to make a difference...
if anybody can give me some information regarding what's going on and how to work around this, it will be greatly appreciated.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你手 2024-11-09 07:07:10

我通常在 RibbonSplitButton 中使用绑定。所以没看到你的问题。您可以在 RibbonSplitButton 中尝试 RibbonGallery 并查看是否有效。

或者使用这样的绑定:

RSB.ItemsSource = new Collection<object> { new { Name = "Paste" }, new { Name = "InsertGlyph" } };

<rb:RibbonGroup Header="ABC">
    <rb:RibbonSplitButton x:Name="RSB">
        <rb:RibbonSplitButton.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </rb:RibbonSplitButton.ItemTemplate>
    </rb:RibbonSplitButton>
</rb:RibbonGroup>

I usually use binding in RibbonSplitButton. So didn't see your issue. You may try RibbonGallery in the RibbonSplitButton and see if it works.

Or use binding like this:

RSB.ItemsSource = new Collection<object> { new { Name = "Paste" }, new { Name = "InsertGlyph" } };

<rb:RibbonGroup Header="ABC">
    <rb:RibbonSplitButton x:Name="RSB">
        <rb:RibbonSplitButton.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </rb:RibbonSplitButton.ItemTemplate>
    </rb:RibbonSplitButton>
</rb:RibbonGroup>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文