代码之家  ›  专栏  ›  技术社区  ›  xanadont

将组合框双向绑定到简单字符串数组

  •  3
  • xanadont  · 技术社区  · 14 年前

    我有一个简单的类,它提供如下状态代码:

    public class StateProvider
    {
        static string[] _codes = new string[]
        {
            "AL",
            "AK",
            ...
        };
    
        public string[] GetAll()
        {
            return _codes;
        }
    }
    

    我的支持视图的模型类看起来有点像这样:

    public class ProjectModel : ChangeNotifier
    {
        StateProvider _states = new StateProvider();
    
        public ProjectModel()
        {
            Project = LoadProject();
        }
    
        ProjectEntity _project;
        public ProjectEntity Project
        {
            get { return _project; }
    
            set
            {
                _project = value;
                FirePropertyChanged("Project");
            }
        }
    
        public string[] States { get { return _states.GetAll(); } }
    }
    

    <ComboBox SelectedValue="{Binding Project.State, Mode=TwoWay}" SelectedValuePath="{Binding RelativeSource={RelativeSource Self}}" ItemsSource="{Binding States}" />
    

    我希望这非常简单,因为我想显示州代码和保存州代码(我不想显示完整的州名称)。因此,我不想在创建一个具有Code和FullName属性的State类时遇到麻烦,避免使用combobox的SelectedValuePath和displaymberpath属性。

    编辑: 在代码中添加了ProjectModel如何更改通知。请注意,ProjectEntity类也会这样做。相信我,这很管用。我省略了它,因为它还继承了一个实体基类,该实体基类通过反射更改通知。双向绑定对任何东西都有效,但对组合框无效。

    3 回复  |  直到 14 年前
        1
  •  1
  •   iCollect.it Ltd    14 年前

    您必须至少在ProjectModel类上实现IPropertyNotifyChanged

    public class ProjectModel : INotifyPropertyChanged
    {
            public event PropertyChangedEventHandler PropertyChanged;
    

    public ProjectEntity Project
    {
        get { return (ProjectEntity)GetValue(ProjectProperty); }
        set { SetValue(ProjectProperty, value); }
    }
    
    // Using a DependencyProperty as the backing store for Project.  
    // This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ProjectProperty =
        DependencyProperty.Register("Project",
                                    typeof(ProjectEntity),
                                    typeof(ProjectModel),
                                    new PropertyMetadata(null,
                                        new PropertyChangedCallback(OnProjectChanged)));
    
    static void OnProjectChanged(object sender, DependencyPropertyChangedEventArgs args)
    {
        // If you need to handle changes
    }
    
        2
  •  1
  •   xanadont    12 年前

    哇,谁想到会变成这样:

    <ComboBox ItemsSource="{Binding States}" SelectedValue="{Binding Project.State, Mode=TwoWay}" />
    

    结果是我在XAML中放置属性的顺序。SelectedValue绑定发生在ItemsSource绑定之前,因此绑定SelectedValue时组合框中没有任何项。

        3
  •  0
  •   Yogesh    14 年前

    将ProjectModel类更改为:

    public class ProjectModel : ChangeNotifier
    {
        StateProvider _states = new StateProvider();
    
        public ProjectModel()
        {
            Project = LoadProject();
    
            States = new ObservableCollection<string>(_states.GetAll());
        }
    
        ProjectEntity _project;
        public ProjectEntity Project
        {
            get { return _project; }
    
            set
            {
                _project = value;
                FirePropertyChanged("Project");
            }
        }
    
        public ObservableCollection<string> States { get; set; }
    }
    

    还要确保 ProjectEntity INotifyPropertyChanged