【问题标题】:Editable WPF ComboBox does not fire PropertyChanged可编辑的 WPF 组合框不会触发 PropertyChanged
【发布时间】:2012-05-07 04:02:05
【问题描述】:

我有一个不可编辑的组合框来显示 SQL 数据库的所有表。

 <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 通知。我的错在哪里?

【问题讨论】:

  • 你确定你的LogTable 是一个依赖属性吗? (除此之外:SqlConnection 在 UI 线程中??)
  • 你想在哪里捕获 propertychanged 事件?

标签: c# wpf combobox propertychanged


【解决方案1】:

SelectedValue的绑定中:

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

否则,绑定会在 DataTable 类型(这是 Combobox 的 DataContext)上查找 LogTable 属性,并且会静默失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多