Microsoft Developer Network > 포럼 홈 > 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일 금요일 오후 7: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일 금요일 오후 7:26XAML formatting screwed up somehow.
    • 편집됨DakkonA 2009년 7월 3일 금요일 오후 7:20
    • 편집됨DakkonA 2009년 7월 3일 금요일 오후 10:43Bad formatting
    •  

답변

  • 2009년 7월 3일 금요일 오후 11: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일 금요일 오후 10: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일 금요일 오후 10: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일 금요일 오후 11: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.