【问题标题】:Double tapped behavior双击行为
【发布时间】:2023-04-07 19:14:01
【问题描述】:

我尝试实现双击行为。 .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# avaloniaui


    【解决方案1】:

    无法说出您正在使用的确切包,但看起来您正在创建类似 Blend 的行为,而样式正在尝试设置附加属性。

    如果是这种情况,那么您要么需要更改您的行为类以使用附加属性,要么在您的样式中设置 Interaction.Behaviors 属性。

    附加属性:

    https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/attached-properties-overview

    使用 Blend SDK 行为:

    <TreeView Items="{Binding Sections, ElementName=ToC, Mode=TwoWay}" 
          SelectedItem="{Binding SelectedSection, ElementName=ToC, Mode=TwoWay}">
        <i:Interaction.Behaviors>
            <commandBehaviors:DoubleTappedBehavior Command="{Binding IncrementCount}" CommandParameter="{Binding}" />
        </i:Interaction.Behaviors>
    </TreeView>
    

    附加属性行为:

    public class DoubleTappedBehavior
    {
        public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(DoubleTappedBehavior), new PropertyMetadata(null, OnCommandChanged));
        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(DoubleTappedBehavior), new PropertyMetadata(null));
    
        private static void OnCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
        {
            var element = target as UIElement;
    
            if (element != null)
            {
                element.DoubleTapped -= OnDoubleTapped;
                element.DoubleTapped += OnDoubleTapped;
            }
        }
    
        private static void OnDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {
            var element = sender as UIElement;
    
            if (element != null)
            {
                var command = GetCommand(element);
                var commandParameter = GetCommandParameter(element);
    
                if (command != null)
                {
                    if (command.CanExecute(commandParameter))
                    {
                        command.Execute(commandParameter);       
                    }
                }
            }
        }
    
        public static void SetCommand(UIElement target, ICommand value) => target.SetValue(CommandProperty, value);
    
        public static ICommand GetCommand(UIElement target) => (ICommand)target.GetValue(CommandProperty);
    
        public static void SetCommandParameter(UIElement target, object value) => target.SetValue(CommandProperty, value);
    
        public static object GetCommandParameter(UIElement target) => target.GetValue(CommandParameterProperty);
    }
    

    【讨论】:

    • 我也尝试了附加属性。我没有得到例外,但它仍然不起作用。 c public static readonly AttachedProperty&lt;ICommand&gt; CommandProperty = AvaloniaProperty.RegisterAttached&lt;DoubleTappedBehavior, TreeViewItem, ICommand&gt;("Command"); public static ICommand GetCommand(TreeViewItem element) { return element.GetValue(CommandProperty); } public static void SetCommand(TreeViewItem element, ICommand value) { element.SetValue(CommandProperty, value); }
    • 在您附加的行为类中,您是否在附加属性上定义了更改的处理程序?
    • avalonia 中没有更改的处理程序。 set 和 get 属性永远不会被调用。
    • 您是否有不能回退到使用 DependencyProperty 而不是 AvaloniaProperty 的原因?至少在目标行为的范围内?
    猜你喜欢
    • 1970-01-01
    • 2020-05-23
    • 2013-11-28
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2021-03-16
    • 2013-04-04
    相关资源
    最近更新 更多