【发布时间】:2014-03-02 03:51:21
【问题描述】:
这篇文章经过完全编辑,以提供更可靠的示例和简化的问题
我希望将 Child 更改的结果反映在 Parent 中,尤其是字段 NumberOfChildrenWithDegrees。如果您选中绑定HasUniversityDegree 和HasHighSchoolDegree 的两个框,则子视图中的HasTwoDegrees 会更新。我想要的是 HasTwoDegrees 的孩子的数量反映在父虚拟机中。
我以为这很简单,但事实证明它不起作用 - 选中两个框不会改变父视图(<TextBlock Text="{Binding NumberOfChildrenWithDegrees,Mode=TwoWay,UpdateSourceTrigger=Explicit}"/> 区域。我怎样才能得到这个数字更改为实时发生?
我为 Windows Phone 创建了一个小示例,它可以直接插入到名为 TestPage 的页面中。只需将 XAML 放在内容网格中,将代码放在代码隐藏 (VB) 中。
XAML:
<Grid x:Name="ContentPanel" Background="{StaticResource PhoneChromeBrush}" Grid.Row="1">
<ListBox x:Name="parentLB" Margin="12,12,12,12" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="0,12,0,0">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Parent Name: "/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Number of Children with Degrees: "/>
<TextBlock Text="{Binding NumberOfChildrenWithDegrees,Mode=TwoWay,UpdateSourceTrigger=Explicit}"/>
</StackPanel>
<ListBox Margin="12,0,0,0" x:Name="group2" ItemsSource="{Binding Children,Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Child Name: "/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
<CheckBox Content="Has High School Degree:" IsChecked="{Binding HasHighSchoolDegree,Mode=TwoWay}"/>
<CheckBox Content="Has University Degree: " IsChecked="{Binding HasUniversityDegree,Mode=TwoWay}"/>
<CheckBox Content="Has Two Degrees? " IsEnabled="False" IsChecked="{Binding HasTwoDegrees}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
代码隐藏:
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Partial Public Class TestPage
Inherits PhoneApplicationPage
Public Sub New()
InitializeComponent()
parentLB.ItemsSource = Grandparent1.ParentGroups
End Sub
Public Function Grandparent1() As GrandParent
Dim ParentGroups As New List(Of Parent)
ParentGroups.Add(Parent1)
ParentGroups.Add(Parent2)
Dim gp As New GrandParent
gp.ParentGroups = ParentGroups
Return gp
End Function
Public Function Parent2() As Parent
Dim p As New Parent With {.Name = "Tom"}
Dim c1 As New Child With {.Name = "Tammy"}
Dim c2 As New Child With {.Name = "Timmy"}
Dim children As New List(Of Child)
children.Add(c1)
children.Add(c2)
p.Children = children
Return p
End Function
Public Function Parent1() As Parent
Dim p As New Parent With {.Name = "Carol"}
Dim c1 As New Child With {.Name = "Carl"}
c1.HasHighSchoolDegree = True
c1.HasUniversityDegree = True
Dim c2 As New Child With {.Name = "Karla"}
Dim children As New List(Of Child)
children.Add(c1)
children.Add(c2)
p.Children = children
Return p
End Function
End Class
Public Class GrandParent
Inherits BindableBase
Public Property ParentGroups As List(Of Parent)
End Class
Public Class Parent
Inherits BindableBase
Public Property Name As String
Private _children As List(Of Child)
Public Property Children As List(Of Child)
Get
Return Me._children
End Get
Set(value As List(Of Child))
Me.SetProperty(Me._children, value)
End Set
End Property
Private _numberOfChildrenWithDegrees As Integer
Public Property NumberOfChildrenWithDegrees As Integer
Get
Return Children.Where(Function(f) f.HasTwoDegrees).Count
End Get
Set(value As Integer)
Me.SetProperty(Me._numberOfChildrenWithDegrees, value)
End Set
End Property
End Class
Public Class Child
Inherits BindableBase
Public Property Name As String
Public ReadOnly Property HasTwoDegrees As Boolean
Get
Return HasHighSchoolDegree AndAlso HasUniversityDegree
End Get
End Property
Private _hasUniversityDegree As Boolean
Public Property HasUniversityDegree As Boolean
Get
Return Me._hasUniversityDegree
End Get
Set(value As Boolean)
Me.SetProperty(Me._hasUniversityDegree, value)
OnPropertyChanged("HasTwoDegrees")
End Set
End Property
Private _hasHighSchoolDegree As Boolean
Public Property HasHighSchoolDegree As Boolean
Get
Return Me._hasHighSchoolDegree
End Get
Set(value As Boolean)
Me.SetProperty(Me._hasHighSchoolDegree, value)
OnPropertyChanged("HasTwoDegrees")
End Set
End Property
End Class
Public MustInherit Class BindableBase
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Function SetProperty(Of T)(ByRef storage As T, value As T,
Optional propertyName As String = Nothing) As Boolean
If Object.Equals(storage, value) Then Return False
storage = value
Me.OnPropertyChanged(propertyName)
Return True
End Function
Protected Sub OnPropertyChanged(Optional propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
【问题讨论】:
标签: c# vb.net mvvm windows-phone-8 binding