繁体   English   中英

在WPF中以编程方式绑定图像的画布点

[英]Programmatically bind an image's canvas point in WPF

在我的程序中,我正在动态创建要添加到WPF窗口中的画布中的图像。 我的问题是:如何将canvas.left和canvas.right绑定到类属性。

如果图像在运行时之前存在,我将在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

不知道如何在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");

多亏了Clemens 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM