我想在雙擊并修改 texbox 中的 selectedItem 后更新串列框專案。它不起作用。
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="450" Width="800">
<Grid>
<ListBox x:Name="lstTextes" SelectedItem="{Binding SelectedText,Mode=TwoWay}" ItemsSource="{Binding ListTexts,UpdateSourceTrigger=PropertyChanged}" Grid.Column="0" Margin="52,54,117,85" MouseDoubleClick="lstTextes_DblClick"/>
<Popup x:Name="popLigText" StaysOpen="False" Grid.ColumnSpan="1" Width="300" IsOpen="false">
<ScrollViewer VerticalScrollBarVisibility="Auto" MaxHeight="500" Background="#F9F9F9" FontSize="11" Foreground="#0E1D31">
<StackPanel>
<TextBox x:Name="TB_LigneText" HorizontalAlignment="Center" Text="{Binding Path=SelectedItem, ElementName=lstTextes, Mode=TwoWay}" Width="300" Height="30" KeyDown="UpdateSelectedItem"/>
</StackPanel>
</ScrollViewer>
</Popup>
</Grid>
</Window>
代碼 vb.net
Imports System.ComponentModel
Class MainWindow
Private Property datasGravures As New Gravures
Sub New()
InitializeComponent()
Me.DataContext = datasGravures
End Sub
Private Sub UpdateSelectedItem(sender As Object, e As KeyEventArgs)
Dim c As TextBox = sender
If e.Key = Key.Return Then
Me.popLigText.IsOpen = False
End If
End Sub
Private Sub lstTextes_DblClick(sender As Object, e As MouseButtonEventArgs)
Dim c As ListBox = sender
Me.popLigText.PlacementTarget = c
Me.popLigText.IsOpen = True
End Sub
End Class
Class Gravures
Implements INotifyPropertyChanged
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Private Property _ListTexts As New List(Of String)
Private Property _selectedText As String
Sub New()
ListTexts.Add("toto")
ListTexts.Add("titi")
End Sub
Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
If Not PropertyChangedEvent Is Nothing Then
RaiseEvent PropertyChanged(Me, e)
End If
End Sub
Public Property ListTexts As List(Of String)
Get
Return _ListTexts
End Get
Set(value As List(Of String))
_ListTexts = value
OnPropertyChanged(New PropertyChangedEventArgs("ListTexts"))
End Set
End Property
Public Property SelectedText As String
Get
Return _selectedText
End Get
Set(value As String)
_SelectedText = value
OnPropertyChanged(New PropertyChangedEventArgs("SelectedText"))
End Set
End Property
End Class
錯誤在哪里?謝謝您的幫助。
文本框與所選專案的文本一起顯示。我修改它,串列(字串)ListText 被修改但文本框沒有更新。
uj5u.com熱心網友回復:
字串是一個物件,是 ListTexts 集合的一個元素。ListBox.SelectedItem 屬性包含一個物件。通過更改 TextBox 中的文本,您可以為該屬性分配一個新字串,即一個新物件。SelectedItem 屬性的邏輯檢查 ItemsSource 集合中是否存在可分配物件。如果它不存在,則它不接受它并重置為 null。
根據您的解釋,您需要實作以下兩個選項之一:
1)使用從 TextBox 接收的字串,首先替換 ListTexts 集合中的字串。并且僅在此之后將這一行添加到 SelectedItem。為此,ListTexts 必須是一個可觀察的集合。
2)或者為 ListTexts 元素創建一個物體。該物體將具有一個字串屬性。在物體中實作 INotifyPropertyChanged 介面。然后您可以將 TextBox 系結到物體的屬性。
<TextBox x:Name="TB_LigneText" HorizontalAlignment="Center"
Text="{Binding Path=SelectedItem.SomeProperty, ElementName=lstTextes, Mode=TwoWay}"
Width="300" Height="30" KeyDown="UpdateSelectedItem"/>
uj5u.com熱心網友回復:
謝謝你這么詳細的回答。我添加了一個帶有 String 屬性的類屬性
Class LineText
Implements INotifyPropertyChanged
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Private Property _Line As String
Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
If Not PropertyChangedEvent Is Nothing Then
RaiseEvent PropertyChanged(Me, e)
End If
End Sub
Public Property Line As String
Get
Return _Line
End Get
Set(value As String)
_Line = value
OnPropertyChanged(New PropertyChangedEventArgs("Line"))
End Set
End Property
End Class
我修改了 ListTexts 屬性
Public Property ListTexts As List(Of LineText)
并在界面中將標簽替換為TextBox
<ListBox x:Name="lstTextes" ItemsSource="{Binding Path=ListTexts}" Margin="133,30,267,204" Width="400" Height="200" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="labline" Text="{Binding Line}" Width="200"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
不再需要彈出視窗來修改文本行。謝謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/528385.html
標籤:wpfVB.net捆绑
