Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bind a WPF control to a VB.net property? [duplicate]

Possible Duplicate:
Data Binding WPF Property to Variable

How would I bind my module1 property to my WPF TextBox1?

WPF code:

<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 code:

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

Below is code that I have been working on based on the feed back I have been getting and the reading I have been doing. /#######Current code in progres (trying 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>

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

MainWindow:

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
like image 425
donbyte Avatar asked Dec 12 '25 12:12

donbyte


1 Answers

You are not setting the DataContext anywhere

WPF has two layers: the data layer and the UI layer. The data layer is null by default, and you can set it by setting the DataContext property of any UI objects. Bindings are used to pull data from the data layer into the UI layer.

So, if you say MainWindow.DataContext = new Class1(), then you are setting the data layer beind MainWindow to a new instance of your Class1 object.

Writing <TextBox Text="{Binding tbProperty}" /> in the XAML is telling WPF to look in the data layer for a property called tbProperty and use it for the Text value of the TextBox.

If you change the tbProperty in your Class1 object being used as the DataContext, that change will also be reflected in TextBox.Text (providing you implemented INotifyPropertyChanged). And if the binding mode is set to TwoWay (default for TextBox.Text), then changes to TextBox.Text will also update the tbProperty in the data layer.

I actually recently posted an overview of the DataContext on my blog if you're interested.

like image 152
Rachel Avatar answered Dec 14 '25 03:12

Rachel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!