1

Its been another long coding day and im probably over looking something. But i cant figure out what ive missed

C# Code in ViewModel

public String MainWindowText { get; set; }
public DelegateCommand CommandClose { get; set; }
public TitlebarViewModel()
{
    MainWindowText = "My Epic Window!";
    CommandClose = new DelegateCommand(CloseMain);
}
public void CloseMain(Object Sender)
{
    App.Current.MainWindow.Close();
}
public class DelegateCommand : ICommand
{
    private readonly Predicate<object> _canExecute;
    private readonly Action<object> _execute;
    public DelegateCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public virtual bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public virtual void Execute(object parameter)
    {
        _execute(parameter);
    }

    public void RaiseCanExecuteChanged()
    {
        if (CanExecuteChanged != null)
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }
    }

    #pragma warning disable 67
    public event EventHandler CanExecuteChanged;
    #pragma warning restore 67
}

Xaml Code:

<Window.DataContext>
    <VM:TitlebarViewModel/>
</Window.DataContext>
<Grid>
    <DockPanel>
        <Image x:Name="Icon" Width="30" Height="30"/>
        <Button x:Name="Close" Content="X" Width="25" Height="25" DockPanel.Dock="Right" Margin="2" Command="{Binding CommandClose}"/>

        <TextBlock x:Name="TitleBarText" Text="{Binding MainWindowText}" TextAlignment="Center" Margin="7"/>
    </DockPanel>
</Grid>

So the MainWindow text box is showing from what i set in the c# constructor so i know the datacontext is working fine. i just cannot figure out why i cannot get the delegated command to fire when clicking on the button.

I know this is probably really stupid. and its something simple but ive been staring at this screen for 50 minutes and im really over looking for the error. especially when i have done this 100 times and its in other controls all over my solution

Thanks guys.

daniele3004
  • 13,072
  • 12
  • 67
  • 75
New Bee
  • 991
  • 1
  • 13
  • 24
  • is there any data binding errors? or any other you may notice in output window? – pushpraj Aug 28 '14 at 05:45
  • not for this specific class. or target element – New Bee Aug 28 '14 at 05:49
  • OK, could you provide us a working sample which can reproduce the same? may we have a look here. – pushpraj Aug 28 '14 at 05:56
  • oh wait i figured it out. i actually got it right. however i had a this.PreviewMouseLeftButtonDown event handler so i could move the window about, this was capturing any events to the button. so thats what caused it. – New Bee Aug 28 '14 at 05:56
  • from the code provided there is nothing wrong, I ran it in a sample app and it worked fine – Rishabh Jain Aug 28 '14 at 05:58
  • @NewBee good find! perhaps you can share your finding as an answer. what was the issue and how you solved it. – pushpraj Aug 28 '14 at 06:09

1 Answers1

0

Okay as requested. this application uses an activeX control. this control has airspace issues. so i have overlayed a window with another window using transparency.

when you do this you need to pretty much rewrite the whole code for moving/dragging or what ever for a window. so i have put in some code to allow for "when i move a window move the over lay with it"

so i was using this code

    private void EventHandlers()
    {
        this.LocationChanged += Titlebar_LocationChanged;
        this.Loaded += Titlebar_Loaded;
        this.PreviewMouseLeftButtonDown += Titlebar_PreviewMouseLeftButtonDown;
        this.PreviewMouseLeftButtonUp += Titlebar_PreviewMouseLeftButtonUp;
        this.MouseDoubleClick += Titlebar_MouseDoubleClick;
    }

however be warned. this event triggers you event handler before it will trigger any of the controls on the form.

i simply changed the event handler to read

    private void EventHandlers()
    {
        this.LocationChanged += Titlebar_LocationChanged;
        this.Loaded += Titlebar_Loaded;
        this.MouseLeftButtonDown += Titlebar_PreviewMouseLeftButtonDown;
        this.PreviewMouseLeftButtonUp += Titlebar_PreviewMouseLeftButtonUp;
        this.MouseDoubleClick += Titlebar_MouseDoubleClick;
    }

this the trigger happens after you press any button within the control. Yes as stupid as this might seem to experienced programmers. even we sometimes have memory blocks. please dont judge me.

New Bee
  • 991
  • 1
  • 13
  • 24