wpf - 如何将 WPF 控件绑定(bind)到 VB.net 属性?

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

<分区>

Possible Duplicate:
Data Binding WPF Property to Variable

如何将我的 module1 属性绑定(bind)到我的 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

下面是我根据收到的反馈和我一直在做的阅读所做的代码。 /#######Current code in progres (try with a class instead of a module)#######/

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>

第 1 类:

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

最佳答案

您没有设置 DataContext任何地方

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

所以,如果你说 MainWindow.DataContext = new Class1() , 那么您将数据层设置为 MainWindow到你的新实例 Class1对象。

写作 <TextBox Text="{Binding tbProperty}" />在 XAML 中告诉 WPF 在数据层中查找名为 tbProperty 的属性并将其用于 Text TextBox 的值(value).

如果您更改 tbProperty在你的Class1对象被用作 DataContext ,该更改也将反射(reflect)在 TextBox.Text 中(假设您实现了 INotifyPropertyChanged )。而如果绑定(bind)模式设置为TwoWay (默认为 TextBox.Text ),然后更改为 TextBox.Text还将更新 tbProperty在数据层。

实际上我最近发布了一个 overview of the DataContext on my blog如果你有兴趣。

关于wpf - 如何将 WPF 控件绑定(bind)到 VB.net 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11673145/

相关文章:

wpf - 我可以在我的应用程序中托管 Visual Studio 2010 编辑器吗?

.net - 如何在单元测试VB.net期间禁止弹出消息框

vb.net - 我想在 Word 文档中生成条形码。我该怎么做呢?

c# - WPF - 集合中的标签文本?

c# - 在哪些情况下我必须在 WPF 中序列化命令的对象?

WPF 为绑定(bind)公开计算属性(作为 DependencyProperty)

javascript - 在 vb.net 中获取确认值

c++ - 在 MFC C++ 中删除空格

c# - 如何将应用程序设置导出到可移植文件中? [C#]

c# - 如何在功能区设计器中更改组的大小?