2012-12-04 36 views
0

在我的程序中,我動態地創建要添加到WPF窗口畫布中的圖像。 我的問題是:如何將canvas.left和canvas.right指向一個類屬性。以編程方式綁定WPF中的圖像畫布點

如果圖像運行時間之前就存在我會做,並將其綁定像這樣的XAML/WPF:

<Image Height="26" HorizontalAlignment="Left" Canvas.Left="{Binding left}" Canvas.Top="{Binding top}" Name="Image1" Stretch="Fill" VerticalAlignment="Top" Width="28" Source="/imageTest;component/Images/blue-pin-md.png" /> 

我在VB.net什麼:

'Create array of images 
Dim myImages(5) as myImage 

For i = 0 to myImages.count - 1 
    myImages(i) = New myImage 
    'set datacontext if I can bind 
    myImages(i).image.DataContext = myImages(i) 
    canvas1.Children.Add(myImages(i).image) 
Next 

MYIMAGE類:

Imports System.ComponentModel 

Public Class myImage 
    Implements INotifyPropertyChanged 

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged 

    Private Sub NotifyPropertyChanged(ByVal info As String) 
     RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info)) 
    End Sub 

    Private Property _image as New Image 
    Private Property _left As Double 
    Private Property _top As Double 
    Private Shared Property r As New Random() 

    Public Sub New() 
     _image.Width = 28 
     _image.Height = 26 
     _image.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/imageTest;component/Images/blue-pin-md.png", UriKind.Relative)) 
     'the below works without binding if I just want to set and leave them in one place but I would like to bind them so that I can move them relative to other data 
     '_left = r.Next(0, System.Windows.SystemParameters.PrimaryScreenWidth) 
     '_top = r.Next(0, System.Windows.SystemParameters.PrimaryScreenHeight) 
    End Sub 

    Public ReadOnly Property image As Image 
     Get 
      Return _image 
     End Get 
    End Property 

    Public ReadOnly Property left As Double 
     Get 
      Return _left 
     End Get 
    End Property 

    Public ReadOnly Property top As Double 
     Get 
      Return _top 
     End Get 
    End Property 

    'a possible move method that would take advantage of the binding 
    Public Sub move() 
     _top += 1 
     _left += 1 
     NotifyPropertyChanged("left") 
     NotifyPropertyChanged("top") 
    End Sub 
+0

「在WPF中看起來像這樣」,究竟是什麼問題?綁定看起來不錯。 – Clemens

+0

@Clemens:我想在不使用WPF的VB.NET代碼中執行此操作。直到運行時纔會創建圖像。 – donL

回答

0

感謝克萊門斯C#代碼,我能得到它的工作。以下是我使用的代碼。 .SetBindings(,)是我的關鍵。

For i = 0 To myImages.Count - 1 
    myImages(i) = New myImage 
    myImages(i).image.DataContext = myImages(i) 
    myImages(i).image.SetBinding(Canvas.LeftProperty, "left") 
    myImages(i).image.SetBinding(Canvas.TopProperty, "top") 
    canvas1.Children.Add(myImages(i).image) 
Next 
1

不知道如何把它寫在VB,但在C#它應該是這樣的:

var leftBinding = new Binding 
{ 
    Path = new PropertyPath("left"), 
    Source = myImages[i] 
}; 
var topBinding = new Binding 
{ 
    Path = new PropertyPath("top"), 
    Source = myImages[i] 
}; 
myImages[i].image.SetBinding(Canvas.LeftProperty, leftBinding); 
myImages[i].image.SetBinding(Canvas.TopProperty, topBinding); 

或許可以簡單地使用的DataContext:

myImages[i].image.DataContext = myImages[i]; 
myImages[i].image.SetBinding(Canvas.LeftProperty, "left"); 
myImages[i].image.SetBinding(Canvas.TopProperty, "top"); 
+0

謝謝!這讓我想到了我的答案。對我來說關鍵在於瞭解.SetBindings。接下來我會在這裏發佈我的VB.Net答案。 – donL