【问题标题】:How do I bind a WPF control to a VB.net property? [duplicate]如何将 WPF 控件绑定到 VB.net 属性? [复制]
【发布时间】:2012-07-26 15:48:57
【问题描述】:

可能重复:
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

以下是我根据收到的反馈和一直在阅读的内容编写的代码。 /#######当前代码正在进行中(尝试使用类而不是模块)#######/

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

【问题讨论】:

  • @EsotericScreenName 我已经看到了这个问题,但我一定错过了一些东西,因为我无法弄清楚如何做到这一点。我的脑子里一定有一些基本的想法是错误的。我会继续研究,但感谢您的帮助。

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


【解决方案1】:

你没有在任何地方设置DataContext

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

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

在 XAML 中写入 &lt;TextBox Text="{Binding tbProperty}" /&gt; 是告诉 WPF 在数据层中查找名为 tbProperty 的属性,并将其用于 TextBoxText 值。

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

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

【讨论】:

  • 谢谢!这个答案和您引用的博客文章开启了对 WPF 和绑定的丰富理解。感谢您,我已经开始尝试更复杂的绑定组合!
  • 关于声明的小注释 - “绑定用于将数据从数据层拉入 UI 层。” .这是不准确的说法,因为您通过 Binding.Mode 属性控制数据流。例如写在这里:geekswithblogs.net/mamta_m/archive/2010/07/04/…
【解决方案2】:

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

【讨论】:

  • 如果我在模块中执行此操作,则会收到错误消息。我只能绑定到类的属性吗?
  • 你应该使用一个类,是的。
  • 如何将 XAML 指向该属性?类是 Class1 属性是 tbBinding 作为字符串。
  • 使用属性名:&lt;TextBox Text="{Binding Path=PropertyName}" /&gt;
  • :-( 我需要添加源吗?还是在某处添加数据上下文?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-12
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多