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