我的公司陷入了 .Net 3.0 困境。我想要解决的任务很简单,我需要将 CheckBoxResolvesCEDAR 的 IsChecked 属性绑定(bind)到我的 Audio 类中的 CompletesCEDARWork 。我读得越多,似乎我必须将 CompletesCEDARWork 声明为依赖属性,但我找不到如何完成此操作的好示例。我找到了这个示例,但是当我粘贴到代码中时,我收到 GetValue 的“未定义”错误,并且我尚未成功弄清楚 MyCode 应该代表什么。任何帮助/示例将不胜感激。
谢谢
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
这是我现在精简的音频类。
Imports System.Xml
导入系统 导入系统.IO 导入 System.Collections.ObjectModel 导入 System.ComponentModel
公共(public)课音频
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 Boolean
Get
Return mCompletesCEDARWork
End Get
Set(ByVal value As Boolean)
mCompletesCEDARWork = value
End Set
End Property
这是我设置绑定(bind)的 XML 数据模板。
<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>
最佳答案
您只能在派生自 DependencyObject
的类中声明依赖项属性。这就是为什么您会收到“GetValue 未定义”的原因。您应该阅读这篇有关依赖属性的基础文章以获取更多信息:http://msdn.microsoft.com/en-us/library/ms752914.aspx .
或者,您可以使用INotifyPropertyChanged而不是 DP 来告诉绑定(bind)值已更改。
关于wpf - 如何在 VB.Net 3.0 中声明依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2840281/