使用 vb.net 的 Silverlight
尊敬的专家们: 我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 LayoutRoot 的 datacontext 属性设置为您的地址。然后在您的 xaml 中执行如下操作:
这是一个简单的示例:
MAinPage
VB 的 XAML 代码隐藏:
VB 简单地址类:
注意,我只设置 DataContext 一次,这是针对 LayoutRoot 的。
Set the datacontext attribute of LayoutRoot to you address. Then in your xaml do something like this:
Here is a simple example:
XAML of MAinPage
VB Code Behind:
VB Simple Address Class:
Notice I am only setting DataContext once and that is for LayoutRoot.