wpf 双向绑定不起作用

发布于 2024-11-26 18:05:18 字数 2505 浏览 1 评论 0原文

 <Grid Name="thisPage">
  <TextBlock Name="tbtb" />
  <ScrollViewer Name="sv4" Visibility="Hidden">
    <ItemsControl ItemsSource="{Binding}">
                 <ItemsControl.ItemTemplate>
                     <DataTemplate>
                       <TextBox Text="{Binding Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextChanged="TextBox_TextChanged"/> 
                     </DataTemplate> 
                 </ItemsControl.ItemTemplate> 
             </ItemsControl>
   </ScrollViewer>
  </Grid>

在 MainWindow.vb 中,我有

movieArray as ObservableCollection(of Movie)

For i As Integer = 0 To 5 
        Me.movieArray.Add(New Movie(i)) 
    Next

Me.sv4.DataContext = Me.movieArray
Me.listBox5.DataContext = Me.movieArray

 Private Sub TextBox_TextChanged(sender As System.Object, e As System.Windows.Controls.TextChangedEventArgs)

        Me.tbtb.Text = ""
        For Each m As Movie In movieArray
            Me.tbtb.Text += p.Title.ToString + " ^ "
        Next
       End Sub

Class Movie
    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

 Property Title As Integer
        Get
            Return Me._title
        End Get
        Set(value As Integer)
            Me._title = value
            If Not (value = _title) Then
                Me._title= value
                NotifyPropertyChanged("Title")
            End If
        End Set
    End Property 

下一页,

 <Grid Name="nextPage" Visibility="Hidden" > 
            <ListBox Name="listBox5" >
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Title}"/>
                    </DataTemplate> 
                </ItemsControl.ItemTemplate> 
            </ItemsControl>
        </ListBox>
        </Grid > 

要更改页面,我只需使用后退、下一页按钮切换此页面和下一页的可见性。

我不确定我做错了什么:-

  1. listbox5 仅显示原始值,而不显示任何更改 文本框。
  2. tbtb,但是能够更新其值

i have

 <Grid Name="thisPage">
  <TextBlock Name="tbtb" />
  <ScrollViewer Name="sv4" Visibility="Hidden">
    <ItemsControl ItemsSource="{Binding}">
                 <ItemsControl.ItemTemplate>
                     <DataTemplate>
                       <TextBox Text="{Binding Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextChanged="TextBox_TextChanged"/> 
                     </DataTemplate> 
                 </ItemsControl.ItemTemplate> 
             </ItemsControl>
   </ScrollViewer>
  </Grid>

in the MainWindow.vb, i have

movieArray as ObservableCollection(of Movie)

For i As Integer = 0 To 5 
        Me.movieArray.Add(New Movie(i)) 
    Next

Me.sv4.DataContext = Me.movieArray
Me.listBox5.DataContext = Me.movieArray

 Private Sub TextBox_TextChanged(sender As System.Object, e As System.Windows.Controls.TextChangedEventArgs)

        Me.tbtb.Text = ""
        For Each m As Movie In movieArray
            Me.tbtb.Text += p.Title.ToString + " ^ "
        Next
       End Sub

Class Movie
    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

 Property Title As Integer
        Get
            Return Me._title
        End Get
        Set(value As Integer)
            Me._title = value
            If Not (value = _title) Then
                Me._title= value
                NotifyPropertyChanged("Title")
            End If
        End Set
    End Property 

for the next page i have,

 <Grid Name="nextPage" Visibility="Hidden" > 
            <ListBox Name="listBox5" >
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Title}"/>
                    </DataTemplate> 
                </ItemsControl.ItemTemplate> 
            </ItemsControl>
        </ListBox>
        </Grid > 

To change pages i just toggle the visibility of thisPage and nextPage using back, next buttons.

IM not sure what im doing wrong as:-

  1. listbox5 shows only the original values, not anything changed by
    textboxes.
  2. tbtb, however is able to update its values

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

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

发布评论

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

评论(2

╰◇生如夏花灿烂 2024-12-03 18:05:18

我认为问题可能是你的“标题”属性设置器。

我是一个 C# 人员,而不是 VB 专家...但 NotifyPropertyChanged 似乎永远不会被调用。

value = _title 始终为 true,因为您刚刚在上一行代码中设置了 Me._title = value。因此,您将永远不会执行 if 语句中的任何代码。

I think the problem might be your 'Title' property setter.

I'm a C# guy, not a VB expert... but it would appear that NotifyPropertyChanged will never get called.

value = _title will always be true because you just set Me._title = value in the previous line of code. Thus you will never execute any of the code in your if statement.

执妄 2024-12-03 18:05:18

为什么你使用 Textchanged 事件以两种方式绑定你不需要那种东西。双向绑定是将值从视图直接绑定到属性以及从属性绑定到视图,

因此不要使用 textchanged 事件并重试。这会起作用。

Why are you using Textchanged evetn in two way binding you dont need kind of stuff. two way binding is directly bind values from your view to property and from property to view

so don't use textchanged event and try again. this will work.

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