• 将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绑定集合元素,再次重申不是为了绑定而绑定。希望给新手一些参考。

    再会。

  • 相关阅读:
    Windows 2012 安装 SQL Server 2012,.Net Framework 3.5安装不成的解决办法
    HTML+CSS 对于英文单词强制换行但不截断单词的解决办法
    删除N天前的文件(夹)与拷贝文件到共享盘的批处理
    npm 常用命令
    使用scrapy crawl name启动一个爬虫时出现的问题
    anaconda和pycharm环境交叉的现象
    list和range()函数中使用反向索引的方法
    关于一些术语的解释
    关于assert和de-assert的解释
    搭建eclipse4.6(neon) + Pydev5.8.0 + python3.6(小版本3.6.1)
  • 原文地址:https://www.cnblogs.com/allofalan/p/2431738.html
Copyright © 2020-2023  润新知