Note that there are some explanatory texts on larger screens.

plurals
  1. POComboBox.SelectedValue not updating from binding source
    text
    copied!<p>Here's my binding source object: </p> <pre><code>Public Class MyListObject Private _mylist As New ObservableCollection(Of String) Private _selectedName As String Public Sub New(ByVal nameList As List(Of String), ByVal defaultName As String) For Each name In nameList _mylist.Add(name) Next _selectedName = defaultName End Sub Public ReadOnly Property MyList() As ObservableCollection(Of String) Get Return _mylist End Get End Property Public ReadOnly Property SelectedName() As String Get Return _selectedName End Get End Property End Class </code></pre> <p>Here is my XAML:</p> <pre><code>&lt;Window x:Class="Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300" xmlns:local="clr-namespace:WpfApplication1" &gt; &lt;Window.Resources&gt; &lt;ObjectDataProvider x:Key="MyListObject" ObjectInstance="" /&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;ComboBox Height="23" Margin="24,91,53,0" Name="ComboBox1" VerticalAlignment="Top" SelectedValue="{Binding Path=SelectedName, Source={StaticResource MyListObject}, Mode=OneWay}" ItemsSource="{Binding Path=MyList, Source={StaticResource MyListObject}, Mode=OneWay}" /&gt; &lt;Button Height="23" HorizontalAlignment="Left" Margin="47,0,0,87" Name="btn_List1" VerticalAlignment="Bottom" Width="75"&gt;List 1&lt;/Button&gt; &lt;Button Height="23" Margin="0,0,75,87" Name="btn_List2" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75"&gt;List 2&lt;/Button&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>Here's the code-behind:</p> <pre><code>Class Window1 Private obj1 As MyListObject Private obj2 As MyListObject Private odp As ObjectDataProvider Public Sub New() InitializeComponent() Dim namelist1 As New List(Of String) namelist1.Add("Joe") namelist1.Add("Steve") obj1 = New MyListObject(namelist1, "Steve") . Dim namelist2 As New List(Of String) namelist2.Add("Bob") namelist2.Add("Tim") obj2 = New MyListObject(namelist2, "Tim") odp = DirectCast(Me.FindResource("MyListObject"), ObjectDataProvider) odp.ObjectInstance = obj1 End Sub Private Sub btn_List1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn_List1.Click odp.ObjectInstance = obj1 End Sub Private Sub btn_List2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn_List2.Click odp.ObjectInstance = obj2 End Sub End Class </code></pre> <p>When the Window first loads, the bindings hook up fine. The ComboBox contains the names "Joe" and "Steve" and "Steve" is selected by default. However, when I click a button to switch the ObjectInstance to obj2, the ComboBox ItemsSource gets populated correctly in the dropdown, but the SelectedValue is set to Nothing instead of being equal to obj2.SelectedName.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload