1

我是 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 不断变化。我想显示当前工作的 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>

这是我的窗户课。

 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

在不更新文本标签的情况下,整个功能运行良好。但我想展示一些东西。我哪里错了

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

4

1 回答 1

1

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

A. 您的 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

附带说明:除非您有充分的理由这样做,否则我建议您创建一个包含要绑定的属性的专用视图模型(类,而不是用户控件)。

于 2013-02-25T10:23:25.180 回答