我無法通過系結將(字串)串列系結為組合框的源。它適用于
cb_ListTexts.ItemsSource = cKiosque.ListTexts
但不與
cb_ListTexts.DataContext = cKiosque
<Window x:Class="MainWindow"
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"
xmlns:local="clr-namespace:BindingWPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ComboBox x:Name="cb_ListTexts" SelectedValue="{Binding Path=ckiosque.ListTexts}" HorizontalAlignment="Left" Height="51" Margin="97,121,0,0" VerticalAlignment="Top" Width="367"/>
<TextBox x:Name="tb_SelectedItem" Text="{Binding Path=SelectedValue, ElementName=cb_ListTexts}" HorizontalAlignment="Left" Height="28" Margin="243,235,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="288"/>
</Grid>
</Window>
Imports System.ComponentModel
Class MainWindow
Private Property cKiosque As New Kiosque
Sub New()
' Cet appel est requis par le concepteur.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
cb_ListTexts.DataContext = cKiosque.ListTexts
'cb_ListTexts.ItemsSource = cKiosque.ListTexts
End Sub
End Class
Class Kiosque
Implements INotifyPropertyChanged
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
Private Property _ListTexts As New List(Of String)
Private Property _SelectedItem 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 SelctedItem As String
Get
Return _SelectedItem
End Get
Set(value As String)
_SelectedItem = value
End Set
End Property
End Class
錯誤在哪里?
uj5u.com熱心網友回復:
DataContext 與 ItemsSource 不同。示例:您將 Grid DataContext 設定為指向您的 Kiosque。然后,每個孩子和子孩子將從您的網格(級聯)繼承他們的 DataContext。
現在,如果找到系結,它將嘗試從您的 Kiosque 物件(稱為 ViewModel)中的 {Binding Path=MyProperty} 將系結應用到匹配的屬性名稱
要除錯,我建議你設定 this.DataContext = Kiosque。在視窗的建構式中。因此,無需設定 DataContext。
嘗試類似的東西
<Grid>
<ComboBox SelectedItem="{Binding SelctedItem}" ItemsSource="{Binding ListTexts}" HorizontalAlignment="Left" Height="51" Margin="97,121,0,0" VerticalAlignment="Top" Width="367"/>
<TextBox Text="{Binding Path=ListTexts}" HorizontalAlignment="Left" Height="28" Margin="243,235,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="288"/>
</Grid>
請注意:您鍵入的是 SelectedItem 而不是 SelectedItem。您不需要使用 SelectedValue,因為您正在系結一個字串。選擇值是系結到專案的子屬性(例如:引擎顯示SelectedCar.Engine的引擎)
uj5u.com熱心網友回復:
有用!謝謝你的幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/522220.html
標籤:VB.netxml捆绑
上一篇:用于從字串列印模式的正則運算式
