1
我试图实现双击行为。该代码的.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);
}
}
的XAML看起来像:
<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'。可以这样设置吗?
我试了一下还带有附加属性。我没有得到例外,但它仍然无法正常工作。 '''c public static readonly AttachedProperty CommandProperty = AvaloniaProperty.RegisterAttached (“Command”); public static ICommand GetCommand(TreeViewItem element) return element.GetValue(CommandProperty);公共静态无效SetCommand(TreeViewItem元素,ICommand值) element.SetValue(CommandProperty,value);公共静态void SetCommand(TreeViewItem元素,ICommand值) {0} } ''' –
Kevin
在你附加的行为类中,你的附属属性上是否有更改过的处理程序? – Eric
阿瓦洛尼亚没有改变的处理程序。 set和get属性不会被调用。 – Kevin