XAML 交互触发器未触发命令

w6lpcovy  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(186)

我遇到窗口的Loaded事件问题,因此我使用NuGet包

我做了一切需要使用这个链接https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/
我的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>

WindowDataContext是主视图模型类:

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

}
正如您在MainViewModel构造函数中看到的那样,LoadDataCommand应该激发LoadData()方法,但我在该行上放置了一个断点,当加载Window时,什么都没有发生。我没有收到任何错误,它根本就不起作用。我是这个概念的新手,所以我不知道哪里出了问题。我认为我使用InteractionTriggers的方式不对,但我可以'I don“我找不到任何能帮助正确使用它的东西。

CommandInterface类别只是实作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;

}
uyto3xhc

uyto3xhc1#

我不知道为什么,但d:DataContext="{d:DesignInstance Type=local:MainViewModel}"这不工作的Loaded事件,所以我设置DataContext的视图到MainViewModel示例在代码背后(InitializeComponents后)它的工作.如果它不工作,只是改变ICommand实现,在这个问题的答案之一:How to use the CanExecute Method from ICommand on WPF

相关问题