轉帖|其它|編輯:郝浩|2011-04-13 14:06:46.000|閱讀 822 次
概述:最近開發的一個項目中我們使用了微軟模式實踐小組的Prism框架(后續會發布一些學習心得),該框架提供了MVVM模式的一種實踐方式。在MVVM模式中View的事件的響應一般通過Command可以實現,在無法使用Command實現時也可以使用附加屬性(Attached Properties)實現。下面貼一段代碼,用來響應ListView的拖放事件。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
最近開發的一個項目中我們使用了微軟模式實踐小組的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>
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自:博客園