2013-02-25 80 views
1

我是新來的WPF和嘗試這樣的事情來更新類中的WPF窗體中的標籤文本。 onChange事件是越來越觸發,但形式wpf標籤文本綁定不工作,我錯了

這裏就沒有得到顯示的是我的課

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> 

,這是我的窗口類。

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 

沒有更新文本標籤整個功能運行良好。但我希望展示一些東西。我出錯的地方

我嘗試通過刪除少量零件到新創建的項目來進行數據綁定。它在那裏工作。所以在這段代碼中有一些錯誤? :`(

+0

你的strComurl是如何設置的?或者如何調用subMainStrt函數? – Manish 2013-02-25 07:48:07

+0

這個mainstrt函數在按鈕上點擊調用 – 2013-02-25 11:08:17

回答

1

我看到了兩個可能的原因,這並不爲你工作

A.請問你OnPropertyChanged方法看起來像

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

B.確保ExtractDetails比如你?呼籲suMainStrt上,是與您的DataContext的情況下,直接從窗口1的構造函數調用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 

作爲一個方面說明:除非你有咕d原因做到這一點,我建議你創建一個專門的viewmodel(類,而不是usercontrol),它包含你想綁定到的屬性。

+0

非常感謝,但是我在按鈕點擊時調用suMainStrt函數並初始化它。和工作。 OnPropertyChanged方法是相同的。甚至在值發生變化時觸發。但由於某種原因它不會在WPF表單中更新。 – 2013-02-25 11:11:47

+0

非常感謝adabyron。正如你所說,它是B點。 我已經在第一個(假設CLsA)中創建了一個類的實例,並再次在按鈕單擊(CLsB)中重新創建了另一個實例,並且正在訪問第一個類(CLsA)以更新值。你的回答讓我看到了這一點。坦克很多。我浪費了整整一天的額外的生產線:(非常感謝你 – 2013-02-25 11:22:46

+0

很高興你能解決它。請標記我的答案爲接受的,如果你覺得它值得的。 – 2013-02-25 13:14:00