Le réseau pour les développeurs > Forums - Accueil > Windows Presentation Foundation (WPF) > What do I need to do to bind a shape's properties to a custom class or structure's properties?
Poser une questionPoser une question
 

TraitéeWhat do I need to do to bind a shape's properties to a custom class or structure's properties?

  • vendredi 3 juillet 2009 19:14DakkonA Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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)
    • ModifiéDakkonA vendredi 3 juillet 2009 19:26XAML formatting screwed up somehow.
    • ModifiéDakkonA vendredi 3 juillet 2009 19:20
    • ModifiéDakkonA vendredi 3 juillet 2009 22:43Bad formatting
    •  

Réponses

  • vendredi 3 juillet 2009 23:18Guenter Schwaiger Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
    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.
    • Marqué comme réponseDakkonA samedi 4 juillet 2009 06:03
    •  

Toutes les réponses

  • vendredi 3 juillet 2009 22:30Guenter Schwaiger Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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.
  • vendredi 3 juillet 2009 22:42DakkonA Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code
    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
    
    

  • vendredi 3 juillet 2009 23:18Guenter Schwaiger Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
    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.
    • Marqué comme réponseDakkonA samedi 4 juillet 2009 06:03
    •  
  • samedi 4 juillet 2009 06:05DakkonA Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Thanks Guenter! The "info" error actually wasn't in my real code, but changing my structures to classes fixed the problem.