Microsoft 开发人员网络 > 论坛主页 > Windows Presentation Foundation (WPF) > What do I need to do to bind a shape's properties to a custom class or structure's properties?
提出问题提出问题
 

已答复What do I need to do to bind a shape's properties to a custom class or structure's properties?

  • 2009年7月3日 19:14DakkonA 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    For some reason my object is not updating with changes in the source. The initial value is bound okay, but updating doesn't seem to occur. Here is an illustrative sample code:

    (See next post for the code; formatting screwed up)
    • 已编辑DakkonA 2009年7月3日 19:26XAML formatting screwed up somehow.
    • 已编辑DakkonA 2009年7月3日 19:20
    • 已编辑DakkonA 2009年7月3日 22:43Bad formatting
    •  

答案

  • 2009年7月3日 23:18Guenter Schwaiger 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复包含代码
    Hi DakkonA,

    thats much better :)

    You have a bug in your NotifyProperyChanged method. You always fireing the event with "info". That says to WPF, hey the info property has changed. But you want "Margin".

    Second, you can not use a structure you have to use a class.

    Public Class CustomStructure
    	Implements System.ComponentModel.INotifyPropertyChanged
    
    	Private MarginPrivate As Double
    
    	Public Property Margin() As Double
    		Get
    			Return MarginPrivate
    		End Get
    		Set(ByVal value As Double)
    			If Not value = MarginPrivate Then
    				MarginPrivate = value
    				NotifyPropertyChanged("Margin")
    			End If
    		End Set
    	End Property
    
    	Private Sub NotifyPropertyChanged(ByVal info As String)
    		RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs(info))
    	End Sub
    
    	Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    End Class
    
    Hope this helps.
    • 已标记为答案DakkonA 2009年7月4日 6:03
    •  

全部回复

  • 2009年7月3日 22:30Guenter Schwaiger 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Hi DakkonA,

    you have to repost your sample code formatted! I have tried to import the VB code in VS but it's impossible to make it run. Try it yourself.

    You can format your VB.Net code with the icon </> (right of the HTML icon) in the editor.


    By the way why do you using a Structure and not a Class for the CustomStructure?

    Hope it helps.
  • 2009年7月3日 22:42DakkonA 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     包含代码
    I added the code formatted using that function, and when I edited my post it broke. And when I tried to fix it, it didn't work. Let me try again here.

    I'm actually not entirely sure of the details of when to use a structure vs. a class, though I read that structures were more efficient, and didn't see any particular drawbacks, at least that I could understand.

    XAML:
    <Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Grid>
            <Rectangle x:Name="TestShape" Stroke="Black" />
            <Button HorizontalAlignment="Left" VerticalAlignment="Bottom" x:Name="ChangeButton">Change</Button>
            <TextBlock HorizontalAlignment="Right" VerticalAlignment="Bottom" x:Name="CurrentMarginTextBlock" />
        </Grid>
    </Window>
    


    Code-Behind:
    Class Window1 
    
        Public CustomStructureInstance As New CustomStructure
    
        Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
            CustomStructureInstance.Margin = 50
            CurrentMarginTextBlock.Text = CStr(CustomStructureInstance.Margin)
    
            Dim MarginBinding As New Binding
            MarginBinding.Source = CustomStructureInstance
            MarginBinding.Path = New PropertyPath("Margin")
            TestShape.SetBinding(Rectangle.MarginProperty, MarginBinding)
    
    
        End Sub
    
        Private Sub ChangeButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ChangeButton.Click
            CustomStructureInstance.Margin = CustomStructureInstance.Margin + 10
            CurrentMarginTextBlock.Text = CStr(CustomStructureInstance.Margin)
        End Sub
    
    End Class
    
    Public Structure CustomStructure
        Implements System.ComponentModel.INotifyPropertyChanged
    
        Private MarginPrivate As Double
    
        Public Property Margin() As Double
            Get
                Return MarginPrivate
            End Get
            Set(ByVal value As Double)
                If Not value = MarginPrivate Then
                    MarginPrivate = value
                    NotifyPropertyChanged("Margin")
                End If
            End Set
        End Property
    
        Private Sub NotifyPropertyChanged(ByVal info As String)
            RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("info"))
        End Sub
    
        Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    End Structure
    
    

  • 2009年7月3日 23:18Guenter Schwaiger 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复包含代码
    Hi DakkonA,

    thats much better :)

    You have a bug in your NotifyProperyChanged method. You always fireing the event with "info". That says to WPF, hey the info property has changed. But you want "Margin".

    Second, you can not use a structure you have to use a class.

    Public Class CustomStructure
    	Implements System.ComponentModel.INotifyPropertyChanged
    
    	Private MarginPrivate As Double
    
    	Public Property Margin() As Double
    		Get
    			Return MarginPrivate
    		End Get
    		Set(ByVal value As Double)
    			If Not value = MarginPrivate Then
    				MarginPrivate = value
    				NotifyPropertyChanged("Margin")
    			End If
    		End Set
    	End Property
    
    	Private Sub NotifyPropertyChanged(ByVal info As String)
    		RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs(info))
    	End Sub
    
    	Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    End Class
    
    Hope this helps.
    • 已标记为答案DakkonA 2009年7月4日 6:03
    •  
  • 2009年7月4日 6:05DakkonA 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Thanks Guenter! The "info" error actually wasn't in my real code, but changing my structures to classes fixed the problem.