Microsoft Developer Network > Domovská stránka fór > Windows Presentation Foundation (WPF) > What do I need to do to bind a shape's properties to a custom class or structure's properties?
Odeslat dotazOdeslat dotaz
 

OdpovědětWhat do I need to do to bind a shape's properties to a custom class or structure's properties?

  • 3. července 2009 19:14DakkonA Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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)
    • UpravenýDakkonA 3. července 2009 19:26XAML formatting screwed up somehow.
    • UpravenýDakkonA 3. července 2009 19:20
    • UpravenýDakkonA 3. července 2009 22:43Bad formatting
    •  

Odpovědi

  • 3. července 2009 23:18Guenter Schwaiger Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     OdpovědětObsahuje kód
    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.
    • Označen jako odpověďDakkonA 4. července 2009 6:03
    •  

Všechny reakce

  • 3. července 2009 22:30Guenter Schwaiger Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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.
  • 3. července 2009 22:42DakkonA Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Obsahuje kód
    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
    
    

  • 3. července 2009 23:18Guenter Schwaiger Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     OdpovědětObsahuje kód
    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.
    • Označen jako odpověďDakkonA 4. července 2009 6:03
    •  
  • 4. července 2009 6:05DakkonA Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    Thanks Guenter! The "info" error actually wasn't in my real code, but changing my structures to classes fixed the problem.