3

可能重复:
将 WPF 属性绑定到变量的数据

如何将我的 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

下面是我一直在根据我得到的反馈和我一直在做的阅读工作的代码。/#######当前代码正在进行中(尝试使用类而不是模块)#######/

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>

第一类:

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
4

2 回答 2

5

你没有设置DataContext任何地方

WPF 有两层:数据层和 UI 层。数据层是null默认的,您可以通过设置DataContext任何 UI 对象的属性来设置它。绑定用于将数据从数据层拉入 UI 层。

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

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

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

如果您有兴趣,我实际上最近在我的博客上发布了一个概述。DataContext

于 2012-07-26T19:35:54.760 回答
0

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

编辑:

下面是Class1从 XAML 绑定到类(也称为“视图模型”)的示例:

<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

于 2012-07-26T17:58:51.810 回答