Mookie
Mookie

Reputation: 35

Command Binding with WPF

Im a complete Noob to this so im having a really hard time wrapping my head around how this works.

Basically I have a Main Page that im using, and within the XAML i have created a menu


What I have is a Document (DummyDoc) that contains a TextBox within it that i am trying to send the find command to.

Ive tried this every which way and googled it but i just cant seem to get it to work for me and could use some help with a push in the right direction

Main form

 <Window>

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:DMC_Robot_Editor"
        xmlns:local="clr-namespace:DMC_Robot_Editor.GUI" 

    <Menu>
    <MenuItem Header="_Edit">
     <MenuItem Header="_Cut"/>
    </MenuItem>
    <MenuItem/>
    <Grid>
     <local:DummyDoc x:Name="_Editor"/>
    </Grid>
    </Window>

That is the main form that i am using. then i have my second document "DummyDoc"

<ad:DocumentContent x:Name="document" x:Class="DMC_Robot_Editor.Controls.DummyDoc"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ad="clr-namespace:AvalonDock;assembly=AvalonDock"
        xmlns:local="clr-namespace:DMC_Robot_Editor.Controls" 
        xmlns:ed="schemas.microsoft.com/expression/2010/drawing"
        Title="Window1" Height="300" Width="300"
        IsVisibleChanged="Is_VisibleChanged"  PropertyChanged="document_PropertyChanged">
    <Grid>

        <Menu >
            <MenuItem Header="_File">
                <MenuItem Header="was here"/>
            </MenuItem>

        </Menu>
        <local:Editor x:Name="source" IsVisibleChanged="Is_VisibleChanged" TextChanged="TextChanged" UpdateFunctions="raiseupdated" />
        <local:Editor x:Name="data" Visibility="Hidden"  IsVisibleChanged="Is_VisibleChanged" TextChanged="TextChanged"      UpdateFunctions="raiseupdated"/>
    </Grid>
    </ad:DocumentContent>

DummyDoc is a window that has an Inherited Editor in it.

<avalonedit:TextEditor
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit" 
             x:Class="DMC_Robot_Editor.Controls.Editor" 
             x:Name="editor"
             mc:Ignorable="d" 
             d:DesignHeight="300"
    d:DesignWidth="300" 

    TextChanged="Text_Changed"
    IsVisibleChanged="raiseUpdate"
    MouseMove="Mouse_Move"
    MouseHover="Mouse_Hover"

    MouseHoverStopped="Mouse_Hover_Stopped" KeyUp="editor_KeyUp">


    </avalonedit:TextEditor> 

My Ultimate Question is how do i use WPF Binding to make the "Cut" Action from the main form initiate the cut() method of the textbox?

I wrote textbox in it because in code behind, im doing the following

  partial class DummyDoc:DocumentContent
    {
    public Editor TextBox{get;set;}
     private void Is_VisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (sender is Editor)
                this.TextBox = sender as Editor;

            if ((VisibilityChanged != null) && (TextBox != null))
                raiseupdated(TextBox, new FunctionEventArgs(this.TextBox.Text));
        }
    }

Upvotes: 0

Views: 642

Answers (1)

Colin Smith
Colin Smith

Reputation: 12550

ElementName looks up elements by looking for an element which is using the string identifier you specify.

Did you put x:Name="local:TextBox" on your TextBox tag?

I think you've got your wires crossed by using "local:TextBox".

For starters...that is the syntax used to refer to an element type within a namespace .... it means "the type TextBox in the local namespace".....it's not valid (or rather doesn't mean the same) in the context you are using....you should just assign an "identifier" string.

So....

CommandTarget="{Binding ElementName=textboxFind}"

...

<TextBox x:Name="textboxFind"  ..... />

would be more appropriate.


UPDATE (in light of question being clarified):

You should specify a "Command" in your menu item which will get raised when you choose that menu item.

Then if the TextEditor has the focus (...and thus is the command target...)....then it should see the Cut command.

I would expect the Avalon Editor to be able to handle the well know "ApplicationCommands" i.e. Cut, Copy, Paste, etc.

<MenuItem Header="_Cut" Command="ApplicationCommands.Cut">

Upvotes: 1

Related Questions