2012-05-06 56 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組合框),以及自動失敗。