【问题标题】:WPF databinding update comboxbox2 based on selection change in combobox1 with MVVMWPF数据绑定更新comboxbox2基于组合框1中的选择更改与MVVM
【发布时间】:2011-02-21 15:36:21
【问题描述】:

我有一个组合框,已绑定到我的视图模型中存在的列表。现在,当用户在该组合框中进行选择时,我想要第二个组合框来更新其内容。

例如,combobox1 是州,combobox2 应该只包含该州的邮政编码。

但在我的情况下,我没有预先定义的组合框 2 列表,我需要从数据库中获取。

另外,如果需要,我可以事先获得 combobox2 的所有潜在值(对于每个 combobox1 值),但如果可以的话,我想避免这种情况。

如何在 WPF 中实现并使用 MVVM?我对整个 wpf\databinding\mvvm 世界还很陌生。

【问题讨论】:

    标签: wpf data-binding mvvm


    【解决方案1】:

    另一种解决方案。近似模型:

    class StateViewModel
    {
        public string StateName
        {
            get {...}
            set {...}
        }
    
        public ObservableCollection<ZipCodeViewModel> ZipCodes
        {
            get {...}
            set {...}
        }
    }
    
    class ZipCodeViewModel
    {
        public string ZipCodeName
        {
            get {...}
            set {...}
        }
    }
    
    class MainViewModel
    {
        public ObservableCollection<StateViewModel> States
        {
            get {...}
            set {...}
        }
    }
    

    还有 XAML:

    <ComboBox ItemsSource="{Binding Path=States}" IsSynchronizedWithCurrentItem="True">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding Path=StateName}"></Label>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    
    <ContentControl Content="{Binding Path=States}">
        <ContentControl.ContentTemplate>
            <DataTemplate>
                <ComboBox ItemsSource="{Binding Path=ZipCodes}">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=ZipCodeName}"></Label>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </DataTemplate>
        </ContentControl.ContentTemplate>
    </ContentControl>
    

    【讨论】:

      【解决方案2】:

      类似于以下内容。请注意,为了示例,代码被大大简化了。实际上,您的 ViewModel 将实现 INotifyPropertyChanged 并在修改属性时引发 PropertyChanged 事件。

      关键是 SelectedState 的设置器。您的 ComboBox 会将其 SelectedValue 属性绑定到 ViewModel 的 SelectedState 属性。当属性更改时,ZipCodes 集合会重新加载,另一个组合框将绑定到该集合。

      class MyViewModel {
      
          public ObservableCollection<string> States {
              get;
              private set;
          }
      
          public ObservableCollection<string> ZipCodes {
              get;
              private set;
          }
      
          public string SelectedState {
              get { return _selectedState; }
              set {
                  _selectedState = value;
                  LoadZipCodes(_selectedState);
              }
          }
      
          public string SelectedZipCode {
              get;
              set;
          }
      
          void LoadZipCodes(string state) {
              // repopulate the ZipCodes property
          }
      
      }
      

      【讨论】:

      • 只是添加一些明显的提示... 1. 在添加新的 Zips 之前清除集合 2. 我肯定会尝试实现异步模式,你不希望屏幕冻结, 直到电话回来。 3. 如果实现异步方法,您可能希望禁用 Zip 组合,直到调用返回。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 2015-02-01
      • 2012-09-25
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多