I can't bind a list (of string) as the source of a combobox via Binding. It works with
cb_ListTexts.ItemsSource = cKiosque.ListTexts
but not with
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
Where is the error ?
CodePudding user response:
DataContext is different from ItemsSource. Exemple : You set your Grid DataContext to point to your Kiosque. Then, each child and subchilds will inherit their DataContext from your Grid (cascading).
Now, if a binding is found, it will try to apply a binding to a matching property name from the {Binding Path=MyProperty} in your Kiosque object (known as a ViewModel)
To debug, I suggest you to set this.DataContext = Kiosque. In the constructor of your window. Thus, no need to set DataContext.
Try with something like
<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>
Be careful : you typed SelctedItem instead of SelectedItem. You don't need to use SelectedValue because you are binding a string. Selected value is to bind to a sub property of the item (eg : Engine to display the engine of SelectedCar.Engine)
CodePudding user response:
It works! Thanks for your help