【问题标题】:Databinding WPF combobox doesn't work twoway数据绑定 WPF 组合框不能双向工作
【发布时间】:2011-04-04 14:07:01
【问题描述】:

我有一个组合框,它通过数据绑定填充了枚举,效果很好。所选项目与属性的绑定也可以正常工作。

我已将属性的绑定设置为 TwoWay,但如果属性 MyDbType 更改,组合框不会更改。

XAML:

<Window.Resources>
    <ObjectDataProvider x:Key="dbEnum" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="utils:DbType" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

<ComboBox Margin="0" Name="comboDbType" VerticalAlignment="Center" Grid.Row="1" Height="25" 
  ItemsSource="{Binding Source={StaticResource dbEnum}}" 
  SelectedItem="{Binding Path=CurrDbSettings.MyDbType, 
    RelativeSource={RelativeSource Mode=FindAncestor, 
      AncestorType={x:Type local:MainWindow}}, 
    Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
  Grid.ColumnSpan="1" Grid.Column="1">
</ComboBox>

有什么提示吗?

【问题讨论】:

    标签: wpf data-binding c#-3.0 combobox


    【解决方案1】:

    属性MyDbType 需要以某种方式告诉绑定系统它已更改。为此,您需要在 CurrDbSettings 对象的类上实现INotifyPropertyChanged 接口,并在MyDbType 属性的设置器中引发PropertyChanged 事件。像这样的:

    public class DbSettings : INotifyPropertyChanged
    {
       ...
       public MyTypeEnum MyDbType
       {
           get { return _myDbType; }
           set 
           { 
               _myDbType = value;
               RaisePropertyChanged("MyDbType");
           }
       }
       ...
    }
    

    【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 2015-12-12
    • 2014-02-03
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多