SJuan76
SJuan76

Reputation: 24895

Combobox does not reflect the initial object value

I have a List of objects, and a few fields inside a Grid. When an object in the List(lvInvoices) is selected, I update the dataBinding of the Grid (lyDetailForm):

private void lvInvoices_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  int index = lvInvoices.SelectedIndex;
  if (index != -1)
  {
    Invoice selectedInvoice = this.ListItems.ElementAt(index);
    lyDetailForm.DataContext = selectedInvoice;
    ((PdfViewer)this.pdfControlHost.Child).File = selectedInvoice.SourceFile.FullName;
  }
}

In lyDetailForms, I have several controls. When I set the DataContext of the grid, the text controls are updated correctly. Yet the combobox appears white until I set it once; after that it is updated correctly when I change the selected item.

<Grid Name="lyDetailForm" Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="2" Grid.Row="1" Margin="10">
  <TextBox Name="tbNif"  Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" Width="100" Margin="5" IsEnabled="{Binding SpId, Converter={x:Static local:SpSentToBooleanConverter.Instance}, ConverterParameter=NEGATE, FallbackValue=False}" Text="{Binding Nif,ValidatesOnExceptions=True,NotifyOnValidationError=True}" Validation.Error="OnEditBoxError"/>
  <ComboBox Name="cbType" Grid.Column="1" Grid.Row="2" Grid.RowSpan="2" Height="23" HorizontalAlignment="Left" Margin="5" VerticalAlignment="Top" Width="100" IsEnabled="{Binding SpId, Converter={x:Static local:SpSentToBooleanConverter.Instance}, ConverterParameter=NEGATE, FallbackValue=False}" Text="{Binding Type, Mode=TwoWay} ItemsSource="{Binding Types}"/>
</Grid>

By the way, Types is a static property from the same invoice object which returns an array of strings String[]. Combobox items are strings.

Any suggestions? Thanks in advance.

Upvotes: 2

Views: 164

Answers (1)

Mohammed A. Fadil
Mohammed A. Fadil

Reputation: 9377

Your are using the cbType ComboBox as TextBox and this is wrong, remove the Text="{Binding Type, Mode=TwoWay} binding and set the binding of the ComboBox selected item to something like this SelectedItem={Binding SelectedType} where SelectedType represent the currently selected Type.

Upvotes: 1

Related Questions