按钮不起作用,无法找到合适的 setter 或 getter
Button will not work, unable to find suitable setter or getter
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;
}
}
并且在 .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" />
按钮事件在我尝试过的任何方式下都不起作用。在这次尝试中它给了我例外
'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'.
如果我使用 Command 而不是 Click,它会编译但按钮会被禁用。
你得到这个异常是因为 Click
是 RoutedEvent
而 OnPanelButtonClickHandler
应该在 *.axaml.cs
后面的代码中。
如果您想从视图中调用视图模型中的函数,您应该使用 Command
属性 并绑定到函数或在视图模型中实现命令。
在您的情况下,当您绑定到命令时按钮处于非活动状态,因为您没有传递所需的参数。这应该有效:
private void OnPanelButtonClickHandler(string parameter)
<Button Command="{Binding OnPanelButtonClickHandler}" CommandParameter="Queue" .../>
您可以在 docs
中找到更多信息
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;
}
}
并且在 .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" />
按钮事件在我尝试过的任何方式下都不起作用。在这次尝试中它给了我例外
'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'.
如果我使用 Command 而不是 Click,它会编译但按钮会被禁用。
你得到这个异常是因为 Click
是 RoutedEvent
而 OnPanelButtonClickHandler
应该在 *.axaml.cs
后面的代码中。
如果您想从视图中调用视图模型中的函数,您应该使用 Command
属性 并绑定到函数或在视图模型中实现命令。
在您的情况下,当您绑定到命令时按钮处于非活动状态,因为您没有传递所需的参数。这应该有效:
private void OnPanelButtonClickHandler(string parameter)
<Button Command="{Binding OnPanelButtonClickHandler}" CommandParameter="Queue" .../>
您可以在 docs
中找到更多信息