2012-07-26 73 views
2

可能重複:
Data Binding WPF Property to Variable如何將WPF控件綁定到VB.net屬性?

我怎麼會我模塊1屬性綁定到我的WPF TextBox1的?

WPF代碼:

<Window x:Class="MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <TextBox Height="23" HorizontalAlignment="Left" Margin="210,146,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" /> 
    </Grid> 
</Window> 

VB.net代碼:

Module Module1 
    ReadOnly Property tbBinding As String 
     Get 
      Return "Success!" 
     End Get 
    End Property 
End Module 

下面是我一直是以飼料回來,我已經越來越和閱讀工作的代碼我一直這樣做。在PROGRES(具有類代替一個模塊試圖) /#######當前代碼#######/

XAML:

<Window x:Class="MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
    <Grid DataContext="Class1"> 
     <TextBox Height="23" HorizontalAlignment="Left" Margin="210,146,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" Text="{Binding Path=tbBinding2}"/> 
     <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="192,74,0,0" Name="Button1" VerticalAlignment="Top" Width="75" /> 
    </Grid> 
</Window> 

的Class1:

Imports System.ComponentModel 

Public Class Class1 
    Implements INotifyPropertyChanged 

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged 

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

    Dim varField As String = String.Empty 

    Public Property tbBinding2 As String 
     Get 
      Return varField 
     End Get 

     Set(value As String) 
      varField = value 
      NotifyPropertyChanged("tbBinding2") 
     End Set 
    End Property 
End Class 

主窗口:

Class MainWindow 

    Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click 
     Dim myClass1 As New Class1 
     myClass1.tbBinding2 = "Success!" 
    End Sub 
End Class 
+0

@EsotericScreenName我已經看到了這個問題,但我必須錯過一些東西,因爲我一直無法弄清楚如何做到這一點。我腦子裏一定有一些根本性的想法。我會繼續研究,但感謝你的幫助。 – donbyte 2012-07-26 18:40:58

回答

4

您沒有設置任何地方DataContext

WPF有兩個層次:數據層和用戶界面層。數據層默認爲null,您可以通過設置任何UI對象的DataContext屬性來設置它。綁定用於將數據從數據層拉入UI層。

所以,如果你說MainWindow.DataContext = new Class1(),那麼你beind MainWindow數據層設置爲您Class1對象的新實例。

寫在XAML <TextBox Text="{Binding tbProperty}" />告訴WPF在數據層中尋找一個叫做tbProperty屬性,並使用它爲TextBoxText值。

如果您更改Class1對象tbProperty被用作DataContext,這種變化也將在TextBox.Text反映(爲您提供實現INotifyPropertyChanged)。如果綁定模式設置爲TwoWay(默認爲TextBox.Text),則更改爲TextBox.Text也將更新數據層中的tbProperty

我實際上最近發佈了一個overview of the DataContext on my blog,如果你有興趣。

+0

謝謝!這個回答和你引用的博客文章開啓了對WPF和綁定的大量理解,我已經開始嘗試更復雜的綁定的組合全部歸功於你! – donbyte 2012-07-26 20:31:22

+0

關於語句的小評論 - 「綁定用於將數據從數據層拉到第e UI層「。這是不準確的聲明,因爲您通過Binding.Mode屬性控制數據流。例如它寫在這裏:http://geekswithblogs.net/mamta_m/archive/2010/07/04/wpf-binding-mode.aspx – Jviaches 2016-07-21 19:45:24

0

您需要實現INotifyPropertyChanged接口。一個例子參考this文章。

編輯:

這裏是從XAML結合Class1類(也稱爲 「視圖模式」)的一個例子:

<Window x:Class="MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:viewModels:"clr-namespace:MyApplication.ViewModels" 
    Title="MainWindow" Height="350" Width="525"> 
    <Window.DataContext> 
     <viewModels:Class1 /> 
    </Window.DataContext> 

    <Grid> 
     <TextBox Height="23" HorizontalAlignment="Left" Margin="210,146,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" Text="{Binding Path=tbBinding2}"/> 
     <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="192,74,0,0" Name="Button1" VerticalAlignment="Top" Width="75" /> 
    </Grid> 
</Window> 

注意,這XAML假定Class1類包含在同一個程序集下的MyApplication.ViewModels命名空間中。

+0

如果我在模塊中執行此操作,則會出現錯誤。我只能綁定到一個類的屬性? – donbyte 2012-07-26 18:42:50

+0

你應該使用一個班級,是的。 – Bernard 2012-07-26 18:48:34

+0

如何將XAML指向該屬性? Class是Class1屬性是tbBinding作爲字符串。 – donbyte 2012-07-26 19:06:43

相關問題