Aymen Ragoubi
Aymen Ragoubi

Reputation: 396

CommandBinding WPF

In my simple application I'm using Command and CommandBinding. My probem is that when I declare Command in my mainWindow like this :

<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>

It's telling me that BooksCommands does not exist in the namespace.

and in the class BooksCommands I declared the namespace:

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;
        }
    }
}

And this is the MainWindow.xaml.cs:

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();
    }
}
}

Could someone help me please.

Upvotes: 0

Views: 3965

Answers (2)

AnjumSKhan
AnjumSKhan

Reputation: 9827

  1. Make BooksCommands class public.
  2. Replace <CommandBinding Command="local:BooksCommands.ShowBooksList" /> with <CommandBinding x:Key="CmdShowBooksListKey" Command="{x:Static local:BooksCommands.ShowBooksList}" />

Upvotes: 1

Danielle
Danielle

Reputation: 490

Try explicitly declaring your BooksCommands class as being public.

Also, occasionally XAML in Visual Studio seems to be confused about finding references and will throw errors. Sometimes these warnings/errors are "fake" and disappear if you try to run your application without trying to successfully build it. I think this is a Visual Studio quirk specifically that I haven't figured out yet.

Upvotes: 0

Related Questions