首页 > 其他分享 >自定义Behavior

自定义Behavior

时间:2024-05-09 11:14:19浏览次数:11  
标签:自定义 OnAttached void AssociatedObject Behavior SelectedIndex

自定义Behavior

实现功能

在鼠标滚轮滚动时,ComboBox的SelectIndex也实现递增和递减

Code

public class ComboxMouseWheelBehavior : Behavior<ComboBox>
{
    protected override void OnAttached()
    {
        AssociatedObject.MouseWheel += ComboxMouseWheel;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.MouseWheel -= ComboxMouseWheel;
    }

    private void ComboxMouseWheel(object sender, MouseWheelEventArgs e)
    {
       if(e.Delta > 0)
        {
            if(AssociatedObject.SelectedIndex >0)
                AssociatedObject.SelectedIndex -= 1;
        }
        else
        {
            if(AssociatedObject.SelectedIndex < AssociatedObject.Items.Count - 1)
                AssociatedObject.SelectedIndex += 1;
        }
    }
}
<ComboBox>
    <i:Interaction.Behaviors>
        <local:ComboxMouseWheelBehavior/>
    </i:Interaction.Behaviors>
</ComboBox>

在将Behavior附加到ComboBox时,AssociatedObject=被附加的DependencyObject,再调用OnAttached方法,在Behavior被分离会调用OnDetaching方法

源码

public void Attach(DependencyObject dependencyObject)
{
    if (dependencyObject != this.AssociatedObject)
    {
        if (this.AssociatedObject != null)
        {
            throw new InvalidOperationException();
        }

        if (Interaction.ShouldRunInDesignMode || !(bool)this.GetValue(DesignerProperties.IsInDesignModeProperty))
        {
            this.WritePreamble();
    -->     this.associatedObject = dependencyObject;
            this.WritePostscript();
        }
    -->   this.OnAttached();
    }
}

public void Detach()
{
 -->   this.OnDetaching();
    this.WritePreamble();
    this.associatedObject = null;
    this.WritePostscript();
}

标签:自定义,OnAttached,void,AssociatedObject,Behavior,SelectedIndex
From: https://www.cnblogs.com/yinyuessh/p/18181678

相关文章

  • Behavior介绍
    WPFBehavior前提条件下载WpfBehavior库-Microsoft.Xaml.Behaviors.Wpftip:b是我对应Behavior的代号xmlns:b="http://schemas.microsoft.com/xaml/behaviors"介绍<b:Interaction.Triggers><b:EventTriggerEventName="Loaded">&l......
  • 自定义一个radio
    html<viewclass="radio-out":style="{'border-color':selectFlag?'blue':''}"><viewclass="radio-in":style="{'background-color':selectFlag?'blue':'......
  • mfc自定义控件的自动布局
    **CBRS_ALIGN_RIGHT是MFC(MicrosoftFoundationClass)中的一个标志,用于指示控件条可以停靠在框架窗口的客户区域右侧**。 在MFC中,窗口布局和控件的管理是一个重要的功能,尤其是在涉及到用户界面设计时。MFC提供了一套完整的机制来允许开发者创建和管理应用程序的界面,包括控......
  • C#多选下拉菜单自定义控件
    C#在winform项目中多选下拉菜单自定义控件。由 ComboBox和 CheckedListBox组合形成。效果: 自定义控件代码MultiComboBox.csusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Data;usingSystem......
  • C++基础-如何引入第三方静态库、动态库或自定义库 摘自 https://blog.csdn.net/u01310
    C++无论是内置库还是第三方库,都需要自己手动进行查找、配置、引入等工作。本文即是帮助完成C++项目对于库、框架如何完成依赖引入达成可调用的目的,重点讲述开发工具VisualStudio中的操作静态库(.lib)静态库引入适用用于大部分无开源的第三方库,开发者不需要关心库的具体实现如何,......
  • 自定义单链表(非循环)的基本接口函数
    文件描述及头文件包含/********************************************************************* 文件名称: 单链表(非循环)的基本接口程序* 文件作者:[email protected]* 创建日期:2024/05/07* 文件功能:对单链表的增删改查功能的定义* 注意事项:No......
  • 自定义单链表(非循环)反转的基本函数接口
    题干structListNode*ReverseList(structListNode*head){if(head==NULL||head->next==NULL){returnhead;}else{structListNode*Phead=head;structListNode*temp=head->next;Phead->next=NULL;......
  • shell 脚本中使用自定义的alias别名
    摘自:https://blog.csdn.net/cscrazybing/article/details/41285287alias,假名,别名,bash的一个内建命令,用来给常用的较长的命令定义个简短的名称。alias命令的基本格式为alias[word[='command']],[]内为可选项。定义word为command的别名。若=’command’部分省略,则输出word......
  • ECharts自定义提示框浮层内容
    因为提示框内容支持字符串模板和回调函数两种形式字符串模板模板变量有{a},{b},{c},{d},{e},分别表示系列名,数据名,数据值等等,但是trigger属性为axis的时候它数据条就很多了,就可以用{a0},{a1},{a2}这样子去拿数据跟数组下标一样(官网有详细示例)示例:在`option`中的`tooltip`里边写......
  • Docker网络:Docker0、容器互联技术--link、自定义网络、实战部署Redis集群
    一、Docker网络●--理解Docker0在干净的Linux环境上安装docker(将docker的所有镜像、容器先删除,干干净净!)实验:1、查看本地网络信息ipaddr可见有三个网卡信息:lo:本地(回环)地址;ens:虚拟机或云服务器(内网)地址;docker0:docker网络地址。问题:docker是如何处理容器网络访问......