将ObservableCollection(Of T) 数据 绑定到 Canvas(2)

上回说到用ItemsContorl装Canvas的方式,给Canvas绑定集合属性。

还是{Prism}的建议,通过AttachProperty给Canvas加一个Helper。

代码

自定义类

  1: Imports System.Collections.ObjectModel
  2: 
  3: Public Class CanvasItemsHelper
  4: 
  5:     Public Shared ReadOnly ItemListProperty As DependencyProperty = _
  6:         DependencyProperty.RegisterAttached("ItemList", GetType(ObservableCollection(Of String)), _
  7:                                             GetType(CanvasItemsHelper), New PropertyMetadata( _
  8:                                                 Nothing, New PropertyChangedCallback(AddressOf OnItemListPropertyChanged)))
  9: 
 10:     Public Shared Sub OnItemListPropertyChanged(ByVal dp As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
 11:         Dim can As Canvas = CType(dp, Canvas)
 12:         If can.Children.Count = 0 Then
 13:             For Each s As String In e.NewValue
 14:                 Dim newTextBlock As New TextBlock
 15:                 newTextBlock.Background = Brushes.LightBlue
 16:                 newTextBlock.Text = s
 17:                 can.Children.Add(newTextBlock)
 18:             Next
 19:         End If
 20:     End Sub
 21: 
 22:     Public Shared Sub SetItemList(ByVal e As UIElement, ByVal o As ObservableCollection(Of String))
 23:         e.SetValue(ItemListProperty, o)
 24:     End Sub
 25: 
 26:     Public Shared Function GetItemList(ByVal e As UIElement) As ObservableCollection(Of String)
 27:         Return e.GetValue(ItemListProperty)
 28:     End Function
 29: 
 30:     Public Sub New()
 31: 
 32:     End Sub
 33: End Class

 

通过Attach属性,将Canvas的Children做处理,这里我为了实验,只是简单的将绑定的数据 加入到Canvas中,实际上应该

做更复杂的处理,随着绑定集合的变化,变化内容。那是实际应用当中的事情了。

 

如果希望自定义类可以在Xaml编辑器中给出local:xxx的提示,则需要满足一个条件,存在Setxxx(arg1,arg2)的格式的

Shared 方法,在C#中为Static关键字。

 

Xaml文中 在 Canvas中加入CanvasItemsHelper属性,并进行绑定。

  1: <Window x:Class="MainWindow" x:Name="mainWindow"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4:     xmlns:local="clr-namespace:AttachProertyTest"
  5:     Title="MainWindow" Height="350" Width="525">
  6:     <Grid>
  7:         <Canvas local:CanvasItemsHelper.ItemList="{Binding ElementName=mainWindow, Path=NameList}"></Canvas>
  8:     </Grid>
  9: </Window>
 10: 

后台代码

  1: 
  2: Imports System.Collections.ObjectModel
  3: 
  4: Class MainWindow
  5: 
  6:     '
  7:     'NameList As ObservableCollection(Of String)
  8:     '
  9:     Private mNameList As ObservableCollection(Of String)
 10:     Public Property NameList() As ObservableCollection(Of String)
 11:         Get
 12:             Return mNameList
 13:         End Get
 14:         Set(ByVal Value As ObservableCollection(Of String))
 15:             mNameList = Value
 16:         End Set
 17:     End Property
 18: 
 19: 
 20:     Public Sub New()
 21:         NameList = New ObservableCollection(Of String)
 22:         NameList.Add("HelloAttachProerty")
 23:         ' 此调用是设计器所必需的。
 24:         InitializeComponent()
 25: 
 26:         ' 在 InitializeComponent() 调用之后添加任何初始化。
 27: 
 28:     End Sub
 29: End Class
 30: 

 

以上,两篇随笔,描述了如何实现 为Canvas绑定集合元素,再次重申不是为了绑定而绑定。希望给新手一些参考。

再会。

posted @ 2012-04-04 10:10  alan0405  阅读(362)  评论(1编辑  收藏  举报