首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CommandBinding WPF

CommandBinding WPF
EN

Stack Overflow用户
提问于 2016-01-30 00:17:11
回答 2查看 2.5K关注 0票数 0

在我的简单应用程序中,我使用了CommandCommandBinding。我的问题是,当我在mainWindow中声明Command时,如下所示:

代码语言:javascript
复制
<RibbonWindow x:Class="BooksDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:BooksDemo"
    mc:Ignorable="d"
    Title="MainWindow" Height="400" Width="600">

<Window.CommandBindings>
    <CommandBinding Command="Close" Executed="OnClose" />
    <CommandBinding Command="local:BooksCommands.ShowBooksList" />
</Window.CommandBindings> 

<Ribbon DockPanel.Dock="Top">
    <Ribbon.QuickAccessToolBar>
        <RibbonQuickAccessToolBar>
            <RibbonButton SmallImageSource="Images/one.png"
                          Command="local:BooksCommands.ShowBook"/>
            <RibbonButton SmallImageSource="Images/list.png" 
                          Command="local:BooksCommands.ShowBooksList"/>
        </RibbonQuickAccessToolBar>
    </Ribbon.QuickAccessToolBar>

    <Ribbon.ApplicationMenu>
        <RibbonApplicationMenu SmallImageSource="Images/books.png" >
            <RibbonApplicationMenuItem Header="Show _Book" />
            <RibbonSeparator />
            <RibbonApplicationMenuItem Header="Exit" Command="Close" />
        </RibbonApplicationMenu>
    </Ribbon.ApplicationMenu>

    <RibbonTab Header="Home">
        <RibbonGroup Header="Clipboard">
            <RibbonButton Command="Paste" Label="Paste"
                LargeImageSource="Images/paste.png" />
            <RibbonButton Command="Cut" SmallImageSource="Images/cut.png" />
            <RibbonButton Command="Copy" SmallImageSource="Images/copy.png" />
            <RibbonButton Command="Undo" LargeImageSource="Images/undo.png" />
        </RibbonGroup>
        <RibbonGroup Header="Show">
            <RibbonButton LargeImageSource="Images/one.png" Label="Book" />
            <RibbonButton LargeImageSource="Images/list.png" Label="Book List" />
            <RibbonButton LargeImageSource="Images/grid.png" Label="Book Grid" />
        </RibbonGroup>
    </RibbonTab>

    <RibbonTab Header="Ribbon Controls">
        <RibbonGroup Header="Sample">
            <RibbonButton Label="Button" />
            <RibbonCheckBox Label="Checkbox" />
            <RibbonComboBox Label="Combo1">
                <Label>One</Label>
                <Label>Two</Label>
            </RibbonComboBox>
            <RibbonTextBox>Text Box</RibbonTextBox>
            <RibbonSplitButton Label="Split Button">
                <RibbonMenuItem Header="One" />
                <RibbonMenuItem Header="Two" />
            </RibbonSplitButton>
            <RibbonComboBox Label="Combo2" IsEditable="False">
                <RibbonGallery SelectedValuePath="Content" MaxColumnCount="1"
                SelectedValue="Green">
                    <RibbonGalleryCategory>
                        <RibbonGalleryItem Content="Red" Foreground="Red" />
                        <RibbonGalleryItem Content="Green" Foreground="Green" />
                        <RibbonGalleryItem Content="Blue" Foreground="Blue" />
                    </RibbonGalleryCategory>
                </RibbonGallery>
            </RibbonComboBox>
        </RibbonGroup>
    </RibbonTab>



    <ListBox DockPanel.Dock="Left" Margin="5" MinWidth="120">
        <Hyperlink Click="OnShowBook">Show Book</Hyperlink>
    </ListBox>
    <TabControl Margin="5" x:Name="tabControl1">
    </TabControl>

</Ribbon>

它告诉我名称空间中不存在BooksCommands

在BooksCommands类中,我声明了名称空间:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace BooksDemo
{
  class BooksCommands
  {
    private static RoutedUICommand showBook;
    public static ICommand ShowBook
    {
        get
        {
            return showBook ?? (showBook = new RoutedUICommand("Show Book",
            "ShowBook", typeof(BooksCommands)));
        }
    }
    private static RoutedUICommand showBooksList;
    public static ICommand ShowBooksList
    {
        get
        {
            if (showBooksList == null)
            {
               showBooksList = new RoutedUICommand("Show Books","ShowBooks",
               typeof(BooksCommands));
               showBook.InputGestures.Add(new KeyGesture(Key.B,Modifierkeys.
               Alt));
            }
            return showBooksList;
        }
    }
}

这是MainWindow.xaml.cs:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Ribbon;

namespace BooksDemo{
public partial class MainWindow : RibbonWindow
{
       public MainWindow()
    {
        InitializeComponent();

    }


    private void OnClose(object sender, ExecutedRoutedEventArgs e)
    {
        Application.Current.Shutdown();
    }
}
}

有人能帮帮我吗?

EN

回答 2

Stack Overflow用户

发布于 2016-01-30 19:29:57

使用<CommandBinding x:Key="CmdShowBooksListKey" Command="{x:Static local:BooksCommands.ShowBooksList}" />制作public.

  • Replace <CommandBinding Command="local:BooksCommands.ShowBooksList" />BooksCommands
票数 1
EN

Stack Overflow用户

发布于 2016-01-30 04:03:52

尝试显式地将BooksCommands类声明为公共类。

此外,Visual Studio中的XAML有时似乎对查找引用感到困惑,并会抛出错误。有时这些警告/错误是“假的”,如果您试图运行您的应用程序而没有尝试成功地构建它,这些警告/错误就会消失。我认为这是Visual Studio的一个怪癖,特别是我还没有弄明白。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35089107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档