使用 vb.net 的 Silverlight

发布于 2024-10-15 04:25:47 字数 6031 浏览 3 评论 0原文

尊敬的专家们: 我正在使用 VS2010、VB.NET、Silverlight 4。 我需要将 UI 控件与 VB 类进行 2 路数据绑定的代码。请找到我正在使用的

xaml

<Grid  x:Name="LayoutRoot" Background="White"   Width="300"  Height="300" Loaded="LayoutRoot_Loaded">
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100"></ColumnDefinition>
        <ColumnDefinition Width="200"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Rectangle  Fill="blue"   Width="100"  Height="10" Grid.ColumnSpan="2" Margin="152,26,148,28"></Rectangle>
    <TextBlock Text="Name" Grid.Row="1" Grid.Column="0"></TextBlock>
    <TextBlock Text="Address 1" Grid.Row="2" ></TextBlock>
    <TextBlock Text="Address 2" Grid.Row="3" Grid.Column="0"></TextBlock>
    <TextBlock Text="City" Grid.Row="4" Grid.Column="0"></TextBlock>
    <TextBlock Text="State" Grid.Row="5" Grid.Column="0"></TextBlock>
    <TextBlock Text="Zipcode" Grid.Row="6" Grid.Column="0"></TextBlock>
    <TextBox x:Name="txtName" Text="{Binding Name, Mode=TwoWay}" Grid.Row="1" Grid.Column="1" Height="20" Width="100"></TextBox>
    <TextBox x:Name="txtAddress1" Text="{Binding Address1, Mode=TwoWay}" Grid.Row="2" Grid.Column="1" Height="20" Width="100"></TextBox>
    <TextBox x:Name="txtAddress2" Text="{Binding Address2, Mode=TwoWay}" Grid.Row="3" Grid.Column="1" Height="20" Width="100"></TextBox>
    <TextBox x:Name="txtCity" Text="{Binding City, Mode=TwoWay}" Grid.Row="4" Grid.Column="1" Height="20" Width="100"></TextBox>
    <TextBox x:Name="txtState" Text="{Binding State, Mode=TwoWay}" Grid.Row="5" Grid.Column="1" Height="20" Width="100"></TextBox>
    <TextBox x:Name="txtZipcode" Text="{Binding Zipcode, Mode=TwoWay}" Grid.Row="6" Grid.Column="1" Height="20" Width="100"></TextBox>

    <Button Grid.Row="7" Grid.Column="0"  Width="50" Content="Save" x:Name="btnSave" Click="btnSave_Click"></Button>
    <Button Grid.Row="7" Grid.Column="1" Width="50" Content="Clear" x:Name="btnClear" Click="btnClear_Click"></Button>
</Grid>

VB 代码:

部分公共类 MainPage 继承 UserControl

Dim address As Address

Public Sub New()
    address = New Address("nameit", "address1", "address2", "Alexandria", "VA", "22314")


    txtName.DataContext = address
    'txtAddress1.DataContext = address
    'txtAddress2.DataContext = address
    'txtCity.DataContext = address
    'txtState.DataContext = address
    'txtZipcode.DataContext = address
    'LayoutRoot.DataContext = address

    InitializeComponent()
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    MessageBox.Show(address.Name + " " + address.Address1 + " " + address.Address2 + " " + address.City + " " + address.State + " " + address.Zipcode)
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    MessageBox.Show("click")
End Sub


Private Sub LayoutRoot_Loaded(ByVal sender As System.Object, ByVal e As RoutedEventArgs)
    'to focus on particular text box - System.Windows.Browser.HtmlPage.Plugin.Focus()   txt_Name.Focus()

End Sub

结束类

地址.vb

导入 System.ComponentModel

公共类地址 实现 INotifyPropertyChanged

Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Private _name As String
Private _address1 As String
Private _address2 As String
Private _city As String
Private _state As String
Private _zipcode As String

Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
    If PropertyChangedEvent IsNot Nothing Then
        RaiseEvent PropertyChanged(Me, e)
    End If
End Sub

Public Property Name() As String
    Get
        Return _name
    End Get
    Set(ByVal value As String)
        _name = value
        OnPropertyChanged(New PropertyChangedEventArgs("Name"))
    End Set
End Property

Public Property Address1() As String
    Get
        Return _address1
    End Get
    Set(ByVal value As String)
        _address1 = value
        OnPropertyChanged(New PropertyChangedEventArgs("Address1"))
    End Set
End Property

Public Property Address2() As String
    Get
        Return _address2
    End Get
    Set(ByVal value As String)
        _address2 = value
        OnPropertyChanged(New PropertyChangedEventArgs("Address2"))
    End Set
End Property

Public Property City() As String
    Get
        Return _city
    End Get
    Set(ByVal value As String)
        _city = value
        OnPropertyChanged(New PropertyChangedEventArgs("City"))
    End Set
End Property

Public Property State() As String
    Get
        Return _state
    End Get
    Set(ByVal value As String)
        _state = value
        OnPropertyChanged(New PropertyChangedEventArgs("State"))
    End Set
End Property

Public Property Zipcode() As String
    Get
        Return _zipcode
    End Get
    Set(ByVal value As String)
        _zipcode = value
        OnPropertyChanged(New PropertyChangedEventArgs("Zipcode"))
    End Set
End Property

Public Sub New(ByVal name As String, ByVal address1 As String, ByVal address2 As String, ByVal city As String, ByVal state As String, ByVal zipcode As String)
    Me.Name = name
    Me.Address1 = address1
    Me.Address2 = address2
    Me.City = city
    Me.State = state
    Me.Zipcode = zipcode

End Sub

结束类

亲切的问候, 库马尔

Dear Experts,
I am using VS2010, VB.NET, Silverlight 4.
I need the code for 2 way data binding the UI controls with VB class .Please find the code which i am working

xaml

<Grid  x:Name="LayoutRoot" Background="White"   Width="300"  Height="300" Loaded="LayoutRoot_Loaded">
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
        <RowDefinition Height="30" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100"></ColumnDefinition>
        <ColumnDefinition Width="200"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Rectangle  Fill="blue"   Width="100"  Height="10" Grid.ColumnSpan="2" Margin="152,26,148,28"></Rectangle>
    <TextBlock Text="Name" Grid.Row="1" Grid.Column="0"></TextBlock>
    <TextBlock Text="Address 1" Grid.Row="2" ></TextBlock>
    <TextBlock Text="Address 2" Grid.Row="3" Grid.Column="0"></TextBlock>
    <TextBlock Text="City" Grid.Row="4" Grid.Column="0"></TextBlock>
    <TextBlock Text="State" Grid.Row="5" Grid.Column="0"></TextBlock>
    <TextBlock Text="Zipcode" Grid.Row="6" Grid.Column="0"></TextBlock>
    <TextBox x:Name="txtName" Text="{Binding Name, Mode=TwoWay}" Grid.Row="1" Grid.Column="1" Height="20" Width="100"></TextBox>
    <TextBox x:Name="txtAddress1" Text="{Binding Address1, Mode=TwoWay}" Grid.Row="2" Grid.Column="1" Height="20" Width="100"></TextBox>
    <TextBox x:Name="txtAddress2" Text="{Binding Address2, Mode=TwoWay}" Grid.Row="3" Grid.Column="1" Height="20" Width="100"></TextBox>
    <TextBox x:Name="txtCity" Text="{Binding City, Mode=TwoWay}" Grid.Row="4" Grid.Column="1" Height="20" Width="100"></TextBox>
    <TextBox x:Name="txtState" Text="{Binding State, Mode=TwoWay}" Grid.Row="5" Grid.Column="1" Height="20" Width="100"></TextBox>
    <TextBox x:Name="txtZipcode" Text="{Binding Zipcode, Mode=TwoWay}" Grid.Row="6" Grid.Column="1" Height="20" Width="100"></TextBox>

    <Button Grid.Row="7" Grid.Column="0"  Width="50" Content="Save" x:Name="btnSave" Click="btnSave_Click"></Button>
    <Button Grid.Row="7" Grid.Column="1" Width="50" Content="Clear" x:Name="btnClear" Click="btnClear_Click"></Button>
</Grid>

VB code:

Partial Public Class MainPage
Inherits UserControl

Dim address As Address

Public Sub New()
    address = New Address("nameit", "address1", "address2", "Alexandria", "VA", "22314")


    txtName.DataContext = address
    'txtAddress1.DataContext = address
    'txtAddress2.DataContext = address
    'txtCity.DataContext = address
    'txtState.DataContext = address
    'txtZipcode.DataContext = address
    'LayoutRoot.DataContext = address

    InitializeComponent()
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    MessageBox.Show(address.Name + " " + address.Address1 + " " + address.Address2 + " " + address.City + " " + address.State + " " + address.Zipcode)
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    MessageBox.Show("click")
End Sub


Private Sub LayoutRoot_Loaded(ByVal sender As System.Object, ByVal e As RoutedEventArgs)
    'to focus on particular text box - System.Windows.Browser.HtmlPage.Plugin.Focus()   txt_Name.Focus()

End Sub

End Class

Address.vb

Imports System.ComponentModel

Public Class Address
Implements INotifyPropertyChanged

Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Private _name As String
Private _address1 As String
Private _address2 As String
Private _city As String
Private _state As String
Private _zipcode As String

Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
    If PropertyChangedEvent IsNot Nothing Then
        RaiseEvent PropertyChanged(Me, e)
    End If
End Sub

Public Property Name() As String
    Get
        Return _name
    End Get
    Set(ByVal value As String)
        _name = value
        OnPropertyChanged(New PropertyChangedEventArgs("Name"))
    End Set
End Property

Public Property Address1() As String
    Get
        Return _address1
    End Get
    Set(ByVal value As String)
        _address1 = value
        OnPropertyChanged(New PropertyChangedEventArgs("Address1"))
    End Set
End Property

Public Property Address2() As String
    Get
        Return _address2
    End Get
    Set(ByVal value As String)
        _address2 = value
        OnPropertyChanged(New PropertyChangedEventArgs("Address2"))
    End Set
End Property

Public Property City() As String
    Get
        Return _city
    End Get
    Set(ByVal value As String)
        _city = value
        OnPropertyChanged(New PropertyChangedEventArgs("City"))
    End Set
End Property

Public Property State() As String
    Get
        Return _state
    End Get
    Set(ByVal value As String)
        _state = value
        OnPropertyChanged(New PropertyChangedEventArgs("State"))
    End Set
End Property

Public Property Zipcode() As String
    Get
        Return _zipcode
    End Get
    Set(ByVal value As String)
        _zipcode = value
        OnPropertyChanged(New PropertyChangedEventArgs("Zipcode"))
    End Set
End Property

Public Sub New(ByVal name As String, ByVal address1 As String, ByVal address2 As String, ByVal city As String, ByVal state As String, ByVal zipcode As String)
    Me.Name = name
    Me.Address1 = address1
    Me.Address2 = address2
    Me.City = city
    Me.State = state
    Me.Zipcode = zipcode

End Sub

End Class

Kind regards,
Kumar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟燃烟灭 2024-10-22 04:25:47

将 LayoutRoot 的 datacontext 属性设置为您的地址。然后在您的 xaml 中执行如下操作:

<TextBox ... Text="{Binding Name}" />

这是一个简单的示例:

MAinPage

<UserControl x:Class="VBSLTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="TextBox1" VerticalAlignment="Top" Width="192" Text="{Binding Name}" />
    </Grid>
</UserControl>

VB 的 XAML 代码隐藏:

Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
        Dim address As Address
        Address = New Address()
        LayoutRoot.DataContext = address
    End Sub

End Class

VB 简单地址类:

Public Class Address

    Private _name As String = "Some Text"

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
End Class

注意,我只设置 DataContext 一次,这是针对 LayoutRoot 的。

Set the datacontext attribute of LayoutRoot to you address. Then in your xaml do something like this:

<TextBox ... Text="{Binding Name}" />

Here is a simple example:

XAML of MAinPage

<UserControl x:Class="VBSLTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="TextBox1" VerticalAlignment="Top" Width="192" Text="{Binding Name}" />
    </Grid>
</UserControl>

VB Code Behind:

Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
        Dim address As Address
        Address = New Address()
        LayoutRoot.DataContext = address
    End Sub

End Class

VB Simple Address Class:

Public Class Address

    Private _name As String = "Some Text"

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
End Class

Notice I am only setting DataContext once and that is for LayoutRoot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文