• MVVM模式下附加属性的使用


    最近开发的一个项目中我们使用了微软模式实践小组的Prism框架(后续会发布一些学习心得),该框架提供了MVVM模式的一种实践方式。在MVVM模式中View的事件的响应一般通过Command可以实现,在无法使用Command实现时也可以使用附加属性(Attached Properties)实现。下面贴一段代码,用来响应ListView的拖放事件。

    在ViewModel中定义附加属性:

    public static bool GetHandleDragEvent(DependencyObject obj)
            {
                return (bool)obj.GetValue(HandleDragEventProperty);
            }

            public static void SetHandleDragEvent(DependencyObject obj, bool value)
            {
                obj.SetValue(HandleDragEventProperty, value);
            }

            // Using a DependencyProperty as the backing store for HandleDragEvent.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty HandleDragEventProperty =
                DependencyProperty.RegisterAttached("HandleDragEvent", typeof(bool),
                typeof(***), new UIPropertyMetadata(false, OnHandleDragEvent));

     
            private static void OnHandleDragEvent(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {。。。} 

    

    在View中添加TreeView控件:

    <Style x:Key="TreeView" TargetType="TreeView" >
                <Setter Property="viewmodels:***.HandleDragEvent" Value="True" />
                <Setter Property="AllowDrop" Value="True" />
    </Style>

    <TreeView Width="150" DockPanel.Dock="Right" Style="{StaticResource TreeView}"/>

  • 相关阅读:
    c/c++设置图片为透明图
    转;说说AngularJS中的$parse和$eval
    转: css3: display:box详解
    转: 深入理解 AngularJS 的 Scope
    转:说说angularjs中的$parse和$eval
    转: 理解AngularJS中的依赖注入
    angularjs学习总结(快速预览版)
    XML基础知识
    读书笔记:js设计模式
    contains 和 ele.compareDocumentPosition确定html节点间的关系
  • 原文地址:https://www.cnblogs.com/wenjingu/p/2013608.html
Copyright © 2020-2023  润新知