如何将WPF控件绑定到VB.net属性?

时间:2012-07-26 15:48:57

标签: wpf vb.net visual-studio-2010 mvvm binding

  

可能重复:
  Data Binding WPF Property to Variable

如何将module1属性绑定到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

2 个答案:

答案 0 :(得分:5)

您没有将DataContext设置为

WPF有两层:数据层和UI层。默认情况下,数据层为null,您可以通过设置任何UI对象的DataContext属性来设置它。绑定用于将数据从数据层提取到UI层。

因此,如果您说MainWindow.DataContext = new Class1(),那么您要将数据层beind MainWindow设置为Class1对象的新实例。

在XAML中编写<TextBox Text="{Binding tbProperty}" />告诉WPF在数据层中查找名为tbProperty的属性,并将其用于Text的{​​{1}}值。

如果您将TextBox对象中的tbProperty更改为Class1,则该更改也会反映在DataContext中(假设您已实施TextBox.Text })。如果绑定模式设置为INotifyPropertyChangedTwoWay的默认设置),则对TextBox.Text的更改也会更新数据层中的TextBox.Text

如果您有兴趣,我实际上最近发布了overview of the DataContext on my blog

答案 1 :(得分:0)

您需要实现INotifyPropertyChanged接口。有关示例,请参阅this文章。

修改

以下是从XAML绑定到类Class1(a.k.a。“视图模型”)的示例:

<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命名空间下的同一程序集中。