我试图实现双击行为。.cs 代码如下所示:
public class DoubleTappedBehavior : Behavior<InputElement>
{
/// <summary>
/// The command.
/// </summary>
public static readonly DirectProperty<DoubleTappedBehavior, ICommand> CommandProperty =
AvaloniaProperty.RegisterDirect<DoubleTappedBehavior, ICommand>(
nameof(Command),
o => o.Command,
(o, v) => o.Command = v);
public ICommand Command
{
get { return GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.DoubleTapped += DoubleTapped;
}
protected override void OnDetaching()
{
AssociatedObject.DoubleTapped -= DoubleTapped;
base.OnDetaching();
}
private void DoubleTapped(object sender, RoutedEventArgs e)
{
if (Command == null)
return;
Command.Execute(null);
}
}
xml 看起来像:
<TreeView Items="{Binding Sections, ElementName=ToC, Mode=TwoWay}"
SelectedItem="{Binding SelectedSection, ElementName=ToC, Mode=TwoWay}">
<TreeView.Styles>
<Style Selector="TreeViewItem">
<Setter Property="commandBehaviors:DoubleTappedBehavior.Command"
Value="{Binding IncrementCount}"/>
<!--<Setter Property="commandBehaviors:DoubleTappedBehavior.CommandParameter"
Value="{Binding}"/>-->
</Style>
</TreeView.Styles>
</TreeView>
我得到了例外:找不到 AvaloniaProperty 'DoubleTappedBehavior.Command'。可以这样设置吗?