将自定义类的属性绑定到View

时间:2017-11-23 18:11:26

标签: wpf vb.net data-binding

我创建了一个UserControl,并在代码隐藏中添加了一个新类:

Public WithEvents Fridolin As New Frog With {.Location = New Point(11, 22)}
Public Class Frog
    Implements INotifyPropertyChanged

    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
    Private Sub NotifyPropertyChanged(Optional ByVal propertyName As String = Nothing)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

    Dim _location As Point
    Property Location As Point
        Get
            Return _location
        End Get
        Set(value As Point)
            _location = value
            NotifyPropertyChanged("Location")
        End Set
    End Property
End Class

正如您所看到的,还有一个青蛙实例。现在,在我的XAML中有一个Path对象,我想绑定到对象“Fridolin”的Location属性。

这是我的XAML

<UserControl x:Class="ucFrogup"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:app="clr-namespace:resi_estimate"
         mc:Ignorable="d" 
         DataContext="{Binding RelativeSource={RelativeSource Self}}"
         d:DesignHeight="360" d:DesignWidth="640">
<UserControl.Resources>
    <ResourceDictionary>
        <StreamGeometry x:Key="ProtoLeaf" >m 451.45 512 c 17.06 0 30.43 -13.86 30.43 -31.56 V 31.55 C 481.87 13.86 468.51 0 451.44 0 A 32.94 32.94 0 0 0 434.99 4.52 L 46.29 229 c -10.13 5.85 -16.18 16 -16.18 27 0 11 6 21.2 16.18 27 l 388.7 224.48 a 32.92 32.92 0 0 0 16.45 4.52 z</StreamGeometry>
        <StreamGeometry x:Key="ProtoFrog">m 51.45 512 c 17.06 0 30.43 -13.86 30.43 -31.56 V 31.55 C 481.87 13.86 468.51 0 451.44 0 A 32.94 32.94 0 0 0 434.99 4.52 L 46.29 229 c -10.13 5.85 -16.18 16 -16.18 27 0 11 6 21.2 16.18 27 l 388.7 224.48 a 32.92 32.92 0 0 0 16.45 4.52 z</StreamGeometry>
    </ResourceDictionary>
</UserControl.Resources>
<Grid>
    <Viewbox Margin="97,10,10,10">

        <Canvas x:Name="Pond" Background="#FF58CFF1" Margin="4" Height="360" Width="640">


            <Path x:Name="Froggy" Height="80" Width="80" Canvas.Left="{Binding Source=Fridolin, Path=Location.X}" Canvas.Top="{Binding Source=Fridolin, Path=Location.Y}" Stretch="Uniform" Fill="#FF43B231" Data="{DynamicResource ProtoFrog}"/>

        </Canvas>
    </Viewbox>
    <Button x:Name="btnGo" Content="Go" HorizontalAlignment="Left" VerticalAlignment="Top" Height="34" Width="78" ></Button>
</Grid>

我错过了什么?我收到此错误信息btw:

System.Windows.Data Error: 40 : BindingExpression path error: 'Location' property not found on 'object' ''String' (HashCode=-1218894399)'. BindingExpression:Path=Location.Y; DataItem='String' (HashCode=-1218894399); target element is 'Path' (Name='Froggy'); target property is 'Top' (type 'Double')

1 个答案:

答案 0 :(得分:1)

我不确定您的代码背后发生了什么,但根据我的了解,请执行以下操作:

  1. 将您的绑定更改为:Canvas.Left="{Binding Path=Fridolin.Location.X}"
  2. 在代码隐藏的构造函数中,将DataContext指定为自身。我在C#工作,所以请在这里跟我说:this.DataContext = this;
  3. 第2步可能是可选的,因为我无法回想起DataContext是否已经分配给自己。

    这将为所有绑定设置DataContext到ucFrogup对象本身,从而使您无需指定源即可进行绑定。源用于绑定到(其他)UI元素,而不是属性。

    我建议任何想要使用WPF阅读MVVM模式的人。它使绑定更简单,更容易阅读。