开发者

How Do you Declare a Dependancy Property in VB.Net 3.0

开发者 https://www.devze.com 2022-12-31 10:03 出处:网络
My company is stuck on .Net 3.0.The task I am trying to tackle is simple, I need to bind the IsChecked property of the CheckBoxResolvesCEDAR to the CompletesCEDARWork in my Audio class.The more I read

My company is stuck on .Net 3.0. The task I am trying to tackle is simple, I need to bind the IsChecked property of the CheckBoxResolvesCEDAR to the CompletesCEDARWork in my Audio class. The more I read about this it appears that I have to declare CompletesCEDARWork as dependancy propert, but I can not find a good example of how this is done. I found this example, but when I pasted into my code I get an "is not defined" error for GetValue and I have not successfully figure out what MyCode is supposed to represent. Any help/examples would be greatly appreciated.

Thanks

Public Shared ReadOnly IsSpinningProperty As DependencyProperty = DependencyProperty.Register("IsSpinning", GetType(Boolean), GetType(MyCode))

Public Property IsSpinning() As Boolean
    Get
        Return CBool(GetValue(IsSpinningProperty))
    End Get
    Set(ByVal value As Boolean)
        SetValue(IsSpinningProperty, value)
    End Set
End Property

Here is my slimed down Audio Class as it stands now.

Imports System.Xml

Imports System Imports System.IO Imports System.Collections.ObjectModel Imports System.ComponentModel

Public Class Audio

Private mXMLString As String
Private mTarpID As Integer
Private mStartTime As Date
Private mEndTime As Date
Private mAudioArray As Byte()
Private mFileXMLInfo As IO.FileInfo
Private mFileXMLStream As IO.FileStream
Private mFileAudioInfo As IO.FileInfo
Private mDisplayText As String
Private mCompletesCEDARWork As Boolean

Private Property CompletesCEDARWork() As Boolea开发者_运维问答n
    Get
        Return mCompletesCEDARWork
    End Get
    Set(ByVal value As Boolean)
        mCompletesCEDARWork = value
    End Set
End Property

And here is my XML Datatemplate where I set the binding.

<DataTemplate x:Key="UploadLayout" DataType="Audio">
                     <Border BorderBrush="LightGray" CornerRadius="8" BorderThickness="1" Padding="10" Margin="0,3,0,0">
                         <StackPanel Orientation="Vertical">
                            <TextBlock Text="{Binding Path=DisplayText}">
                            </TextBlock>
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                <TextBlock Text="TARP ID" VerticalAlignment="Center"/>

                                <ComboBox x:Name="ListBoxTarpIDs"
                                         ItemsSource="{Binding Path=TarpIds}" 
                                          SelectedValue="{Binding Path=TarpID}"
                                          BorderBrush="Transparent"
                                          Background="Transparent" >
                                </ComboBox>

                            </StackPanel>
                            <CheckBox x:Name="CheckBoxResolvesCEDAR" 
                                      Content="Resolves CEDAR Work"
                                IsChecked="{Binding ElementName=Audio,Path=CompletesCEDARWork,Mode=TwoWay}"/>                                    
                                       </StackPanel>   
                     </Border>
                 </DataTemplate>


You can only declare dependency properties in classes that derive from DependencyObject. That's why you get a "GetValue is not defined". You should read this this fundamental article about dependency properties for further information: http://msdn.microsoft.com/en-us/library/ms752914.aspx.

Alternatively, you can you use INotifyPropertyChanged instead of a DP to tell the binding that the value has changed.


Here is the code I used to implement INotifyPropertyChanged

Public Class Audio
Implements INotifyPropertyChanged

Private mCompletesCEDARWork As Boolean

Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

Public Property CompletesCEDARWork() As Boolean
    Get
        Return mCompletesCEDARWork
    End Get
    Set(ByVal value As Boolean)
        mCompletesCEDARWork = value
        NotifyPropertyChanged("CompletesCEDARWork")
    End Set
End Property

Private Sub NotifyPropertyChanged(ByVal info As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消