Microsoft Developer Network >
Página principal de foros
>
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?
- 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)
Respuestas
- 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.
Hope this helps.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
- Marcado como respuestaDakkonA sábado, 04 de julio de 2009 6:03
Todas las respuestas
- 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. - 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
- 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.
Hope this helps.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
- Marcado como respuestaDakkonA sábado, 04 de julio de 2009 6:03
- Thanks Guenter! The "info" error actually wasn't in my real code, but changing my structures to classes fixed the problem.

