0

SQL データベースのすべてのテーブルを表示するための編集できない ComboBox があります。

 <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>

含まれている UserControl のプロパティは次のようになり、INotifyPropertyChanged も実装します。

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

次のデータ バインディングを使用して ComboBox を埋めます。

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

しかし、ComboBox の選択された値を変更しても、PropertyChanged 通知を受け取りません。私のせいはどこですか?

4

1 に答える 1

2

のバインディングSelectedValue:

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

それ以外の場合、バインディングは型 (コンボボックスの DataContext) のLogTableプロパティを探しており、暗黙のうちに失敗します。DataTable

于 2012-05-06T20:02:05.557 に答える