按钮将行不通,找不到合适的设置器或Getter

发布于 2025-01-25 01:43:54 字数 1988 浏览 3 评论 0原文

public class MainWindowViewModel : ViewModelBase
{
    public MainWindowViewModel()
    {

        Queue = new QueuePanelViewModel();
        Merge = new MergePanelViewModel();

        CurrentQueuePanel ??= new QueuePanel();
        CurrentMergePanel ??= new MergePanel();
        _selectedView = CurrentQueuePanel;

    }

    public QueuePanelViewModel Queue { get; }
    public MergePanelViewModel Merge { get; }

    private UserControl _selectedView;
    public UserControl SelectedView
    {
        get
        {
            return _selectedView;
        }
        set
        {
            _selectedView = value;
        }
    }

    private static QueuePanel CurrentQueuePanel { get; set; }
    private static MergePanel CurrentMergePanel { get; set; }



    private void OnPanelButtonClickHandler(object sender, RoutedEventArgs e)
    {
        switch (((Button)sender).Tag)
        {
            case "Queue":
                SelectedView = CurrentQueuePanel;
                break;
            case "Merge":
                SelectedView = CurrentMergePanel;
                break;
            default:
                ((Button)sender).Content = "Somethin went wrong...";
                break;

        }
        e.Handled = true;
    }
}

在.xaxaml中

 < button tag =“ queue” click =“ {binding onpanelButtonClickHandler}” clickmode =“ pers” margin =“ margin =” 0“” grid =“ 0” grid.column =“ 0” verticallignment =“ “ hixontalAlignment =“ stract” content =“ queue” class =“ btn” />
 

按钮事件将无法以我尝试过的任何方式工作。在这种尝试中,它给了我例外

'找不到合适的设置器或加法器以进行属性单击Avalonia.controls:avalonia.controls.button for grignt avalonia.markup:avalonia.data.binding,可用的setter参数参数列表是:: system.eventhandler`1 [[[avalonia.interactivity.RoutedEventargs,avalonia.Interactivity,版本= 0.10.12.0,culture =中性,publicKeytoken = C8D484A7012F9A8B]线号'40'和线位置'26'。

如果我使用命令而不是单击,则将其编译,但是该按钮将被禁用。

public class MainWindowViewModel : ViewModelBase
{
    public MainWindowViewModel()
    {

        Queue = new QueuePanelViewModel();
        Merge = new MergePanelViewModel();

        CurrentQueuePanel ??= new QueuePanel();
        CurrentMergePanel ??= new MergePanel();
        _selectedView = CurrentQueuePanel;

    }

    public QueuePanelViewModel Queue { get; }
    public MergePanelViewModel Merge { get; }

    private UserControl _selectedView;
    public UserControl SelectedView
    {
        get
        {
            return _selectedView;
        }
        set
        {
            _selectedView = value;
        }
    }

    private static QueuePanel CurrentQueuePanel { get; set; }
    private static MergePanel CurrentMergePanel { get; set; }



    private void OnPanelButtonClickHandler(object sender, RoutedEventArgs e)
    {
        switch (((Button)sender).Tag)
        {
            case "Queue":
                SelectedView = CurrentQueuePanel;
                break;
            case "Merge":
                SelectedView = CurrentMergePanel;
                break;
            default:
                ((Button)sender).Content = "Somethin went wrong...";
                break;

        }
        e.Handled = true;
    }
}

And in the .axaml

  <Button Tag="Queue" Click="{Binding OnPanelButtonClickHandler}" ClickMode="Press" Margin="0" Grid.Row="0" Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Content="Queue" Classes="btn" />

The button event will not work in any fashion I have tried. In this attempt It gives me the exception

'Unable to find suitable setter or adder for property Click of type Avalonia.Controls:Avalonia.Controls.Button for argument Avalonia.Markup:Avalonia.Data.Binding, available setter parameter lists are:
System.EventHandler`1[[Avalonia.Interactivity.RoutedEventArgs, Avalonia.Interactivity, Version=0.10.12.0, Culture=neutral, PublicKeyToken=c8d484a7012f9a8b]] Line 40, position 26.' Line number '40' and line position '26'.

If I use a Command instead of Click, it will compile however the button becomes disabled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半寸时光 2025-02-01 01:43:55

您正在获得此例外,因为单击routedeventonpanelButtonClickHandler应该在*。axaml.cs代码中在后面。

如果要从视图中调用视图模型中的函数,则应使用命令属性并绑定到函数或在视图模型中实现命令。

在您的情况下,当您绑定到命令时,按钮是不活动的,因为您没有传递所需的参数。这应该有效:

private void OnPanelButtonClickHandler(string parameter)
<Button Command="{Binding OnPanelButtonClickHandler}" CommandParameter="Queue" .../>

您可以在

You are getting this exception because Click is the RoutedEvent and OnPanelButtonClickHandler should be in the *.axaml.cs code behind.

If you want to call the function in your view model from the view you should use Command property and bind to the function or implement a command in your view model.

In your case the button is inactive when you bind to the command because you do not pass the required parameters. This should work:

private void OnPanelButtonClickHandler(string parameter)
<Button Command="{Binding OnPanelButtonClickHandler}" CommandParameter="Queue" .../>

You can find more information in the docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文