2012-05-06 53 views
0

我有一个不可编辑的组合框来显示和SQL数据库的所有表。可编辑的WPF组合框不会触发PropertyChanged

<ComboBox Grid.Column="1" 
         Grid.Row="2" 
         Height="23" 
         Margin="3,3,3,3" Name="cbLogTable" VerticalAlignment="Top" 
         ItemsSource="{Binding}" 
         TextSearch.TextPath="TABLE_NAME" 
         SelectedValue="{Binding Path=LogTable, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, ValidatesOnDataErrors=True}" 
         > 
       <ComboBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <TextBlock Text="{Binding Path=TABLE_NAME}"/> 
         </StackPanel> 

        </DataTemplate> 
       </ComboBox.ItemTemplate> 
      </ComboBox> 

含有用户控件的属性看起来是这样,也实现了INotifyPropertyChanged的:

public string LogTable 
    { 
     get 
     { 
      return _logTable; 
     } 
     set 
     { 
      if (_logTable == value) return; 
      _logTable = value; 
      OnPropertyChanged("LogTable"); 
     } 
    } 

我用下面的数据绑定来填充组合框:

private void UpdateLogTable() 
    { 
     var connection = new SqlConnection(_connectionString); 
     connection.Open(); 
     DataTable t = connection.GetSchema("Tables"); 
     cbLogTable.DataContext = t; 
     connection.Close(); 
    } 

但我不在更改ComboBox的选定值时不会收到PropertyChanged通知。我的错在哪里?

+0

你确定你的'LogTable'是一个依赖属性吗? (除此之外:UI线程中的'SqlConnection') – Vlad

+0

你在哪里试图捕获propertychanged事件? – 2012-05-06 19:09:05

回答

2

SelectedValue的结合:

SelectedValue="{Binding Path=LogTable, 
         UpdateSourceTrigger=PropertyChanged, 
         Mode=TwoWay, 
         ValidatesOnDataErrors=True, 
         RelativeSource={RelativeSource FindAncestor, 
             AncestorType={x:Type UserControl}}}" 

否则,结合正在寻找在DataTable类型LogTable属性(它是在DataContext组合框),以及自动失败。