wpf标签文本绑定不起作用,我哪里出错了

时间:2013-02-25 04:24:25

标签: wpf vb.net data-binding

我是WPF的新手,并尝试使用类似的方法从类中更新WPF表单中的标签文本。 onchange事件被触发,但没有显示在表单上

这是我的班级

Public Class ExtractDetails
Inherits UserControl
Implements INotifyPropertyChanged

Private _prdFrstName as string
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Public Property PrdFrstName() As String
    Get
        Return _prdFrstName
    End Get
    Set(ByVal value As String)
        If _prdFrstName <> value Then
            _prdFrstName = value
            Me.OnPropertyChanged("PrdFrstName")
        End If
    End Set
End Property

Public Sub suMainStrt()
    PrdFrstName = strComurl     ''contyains teh URL to nagigate to
    webBrwFrst = New WebBrowser
    webBrwFrst.Navigate(New Uri(strComurl))
    Call extract(webBrwFrst, strComurl)
end sub

结束课

当我从excel文件中获取值并为每个URL循环时,url会不断变化。 我想显示当前正在使用的网址

这是我的XAML

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Avenet Prduct Description Extractor" Height="396.627" Width="588.123" Background="AliceBlue" Icon="LGIcon.ico">
<Grid Height="341.077" Width="567.721" Background="AliceBlue">

<StackPanel Margin="170.225,226.418,3.143,0" Name="StackPanel1" Height="97.994" VerticalAlignment="Top">
        <Label Height="30.906" Name="lblCrntSt1" Content="{Binding Path=PrdFrstName, UpdateSourceTrigger=PropertyChanged}" Width="161" BorderThickness="2" BorderBrush="AliceBlue" Background="Red" Foreground="White" FontSize="13"></Label>

    </StackPanel>
    </Grid>

这是我的Windows课程。

 Class Window1
 Dim clsIniti As New ExtractDetails
 Public Sub New()
    ' This call is required by the Windows Form Designer.
    InitializeComponent()
    'clsIniti = New ExtractDetails
    Me.DataContext = clsIniti
 End Sub    
 end class

没有更新文本标签,整个功能都运行良好。但我希望展示一些东西。哪里出错了

我通过删除几个部分到新创建的项目来尝试数据绑定。它在那里工作。所以这段代码有些不对劲??? :`(

1 个答案:

答案 0 :(得分:1)

我看到两个可能的原因,这对你不起作用。

一个。你的OnPropertyChanged方法是怎样的?

' Correct implementation:
Private Sub OnPropertyChanged(propertyName As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub

B中。确保调用suMainStrt的ExtractDetails实例与DataContext实例相同。通过直接从Window1的构造函数调用suMainStrt来测试它:

Class Window1
    Dim clsIniti As New ExtractDetails
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        'clsIniti = New ExtractDetails
        Me.DataContext = clsIniti

        ' test (if this works, your problem is B.)
        clsIniti.suMainStrt()
    End Sub    
End Class

作为旁注:除非您有充分的理由这样做,否则我建议您创建一个包含要绑定的属性的专用viewmodel(类,而不是usercontrol)。