0
votes

This is my first go 'round with WPF bindings of this nature so I'm sure I'm doing something silly.

Background / Setup

I have the following:

  • A ViewModel (MainWindowViewModel)
  • A bound Datacontext
    • in the Window definition, DataContext="{StaticResource MainWindowViewModel}"
  • In the viewmodel, a list:
    • readonly List<string> _sqlServerChoices = new List<string>{"DEV", "SPDEV", "SPSQL", "SQL2008"};
  • A viewmodel property exposing the list:
    • public List<string> SqlServerChoices{get { return _sqlServerChoices; }}
  • A ComboBox for selecting the SQL Server
  • A property called "Settings" which is a custom type, UploaderSettings, which has collections of database settings, sharepoint settings, etc.
  • A property called SqlServerHasBeenEntered, which is a boolean that returns whether the
  • A text box that should be enabled or disabled based on whether a SQL

The Goal

  • I would like to have the choices for the items in the combobox (the ItemsSource) to be set to the list of SQL Servers. This appears to work fine.
  • I would like the selected element of the list to be a two-way binding to the Settings.DatabaseSettings.SqlServer property on the ViewModel.
  • When this property is updated, I would like to fire OnPropertyChanged for the SqlServerHasBeenEntered property.
  • I would like to have the text box enabled or disabled based on whether the SqlServerHasBeenEntered property is true or false.

The Problem

The binding appears to be failing silently. I see the combo box items, but when I select them, nothing is changing and it appears the onpropertychanged event handler isn't being called.

The Code So Far

The ComboBox XAML definition:

<ComboBox Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" 
                          ItemsSource="{Binding SqlServerChoices}" 
                          SelectedValue="{Binding Settings.DatabaseSettings.SqlServer, 
                                Mode=TwoWay, 
                                UpdateSourceTrigger=PropertyChanged }">

The Boolean value for whether or not the SQL Server field has text:

public bool SqlServerHasBeenEntered
{
    get
    {
        return !String.IsNullOrEmpty(Settings.DatabaseSettings.SqlServer);
    }
}

The Property changing handler:

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
1
did you put the breakpoint in setter of Settings.DatabaseSettings.SqlServer and see if it hits when you select value in combo? also i am assuming Settings.DatabaseSettings.SqlServer is not automatic property?Nitin

1 Answers

0
votes

First, use the ComboBox.SelectedItem property to bind to your Settings.DatabaseSettings.SqlServer property (instead of SelectedValue). Then add the IsEnabled property binding:

<ComboBox IsEnabled="{Binding SqlServerHasBeenEntered}" ... />

Finally, you need to call OnPropertyChanged("SqlServerHasBeenEntered"); from wherever you can when you want it to update. (This can be called from another related property setter that gets updated at a certain time, or just from any method).