1

I have an issue with the Loaded event of the window, so I am using NuGet package enter image description here

I did everything required to use the package from this link https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/

My xaml:

<Window x:Class="TestDynamicWindow.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:b="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:TestDynamicWindow" d:DataContext="{d:DesignInstance Type=local:MainViewModel}"        
    mc:Ignorable="d"
    Title="UserWindow" Height="450" Width="800"
    ResizeMode="NoResize"
    Background="Bisque"   

    >

<b:Interaction.Triggers>
    <b:EventTrigger EventName="Loaded">
        <b:InvokeCommandAction
            CommandParameter="{Binding ElementName=ButtonsListBox, Path=Items.Count}"
            Command="{Binding LoadDataCommand}"/>
    </b:EventTrigger>
</b:Interaction.Triggers>

Window's DataContext is MainViewModel class:

public class MainViewModel
{
    private readonly string path = $"{Environment.CurrentDirectory}\\LogInModels.xml";
    public ObservableCollection<LinkModel> linkModels { get; set; } = new ObservableCollection<LinkModel>();
    public ObservableCollection<LogInModel> LogInModels { get; set; }
    public ICommand LoadDataCommand { get; set; }
    public ICommand AddLinkCommand { get; set; }
    public MainViewModel()
    {
        this.LoadDataCommand = new CommandInterface(LoadData, CanLoadData);
        this.AddLinkCommand = new CommandInterface(AddLink, CanAddLink);
    }


    #region LoadDataMethods
    public void LoadData(object parameter)
    {
        SaveOrGetData saveOrGet = new SaveOrGetData(path);
        LogInModels = saveOrGet.GetData();

        for(int i = 0; i < LogInModels.Count; i++)
        {
            LinkModel lm = new LinkModel(parameter);
            linkModels.Add(lm);
        }
    }

    public bool CanLoadData(object parameter)
    {
        return true;
    }

}

As you can see in the MainViewModel constructor LoadDataCommand should fire LoadData() method, but I put a breakpoint on that line, and when the Window is loaded nothing happens. I don't get any error, it just simply doesn't work. I am new to this concept so I don't know what is going wrong. I think I am using InteractionTriggers in the wrong way but can't find anything that would help to use it in a proper way.

CommandInterface class is just class that implements ICommand

class CommandInterface : ICommand
{
    Action<object> executeMethod;
    Func<object, bool> canExecuteMethod;

    public CommandInterface(Action<object> executeMethod, Func<object, bool> canExecuteMethod)
    {
        this.executeMethod = executeMethod;
        this.canExecuteMethod = canExecuteMethod;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        executeMethod(parameter);
    }

    public event EventHandler CanExecuteChanged;


}
NoName
  • 181
  • 8
  • 1
    You should post your `CommandInterface` implementation if you want anyone to be able to point out your mistake. – mm8 Aug 15 '22 at 12:34
  • @mm8 So, now I added my implementation. Can you guide me to links that would explain to me the differences between ICommand implementations and which implementation I should use in my app? – NoName Aug 17 '22 at 17:14
  • I can't reprodude your issue. The command should get executed. – mm8 Aug 18 '22 at 08:25
  • You mean everything is okay with command implementation ? – NoName Aug 18 '22 at 08:38
  • The code you have posted seems okay. – mm8 Aug 18 '22 at 08:39

1 Answers1

1

I don't know why, but d:DataContext="{d:DesignInstance Type=local:MainViewModel}" this doesn't work for Loaded event, so after I set DataContext of View to MainViewModel instance in code behind(after InitializeComponents) it worked. If it doesn't work just change ICommand implementation to one that is in the answer of this question: How to use the CanExecute Method from ICommand on WPF

NoName
  • 181
  • 8