【问题标题】:fill combobox based on user choice of filename根据用户选择的文件名填充组合框
【发布时间】:2015-08-29 16:52:07
【问题描述】:

我刚刚开始深入了解 wpf mvvm,并且在使用数据库表动态获取所请求文件的路径以填充组合框之后,很难有一点点很好地绑定到组合框。 这就是我现在完成的:

视图模型

public class ComboViewModel: ViewModelBase, INotifyPropertyChanged
{
    private TableList tableList1;
    private TableList tableList2;

    public TableList TableList1
    {
        get
        {
            return tableList1;
        }
        set
        {
            if (tableList1 != value)
            {
                tableList1 = value;
                OnPropertyChanged("TableList1");
            }
        }
    }
    public TableList TableList2
    {
        get 
        {
            return tableList2; 
        }
        set
        {
            if (tableList2 != value)
            {
                tableList2 = value;
                OnPropertyChanged("TableList2");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

型号

public class TableList
{
    public List<string> TBL = null; 
    public TableList()
    {

    }
    public List<string> TablesList(string mdbDir)
    {
        DbConnection connection;
        DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
        DataTable userTables = null;
        List<string> mdbTblList = new List<string>();
        connection = factory.CreateConnection();
        connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbDir;
        string[] restrictions = new string[4];
        restrictions[3] = "Table";
        try
        {
            connection.Open();

            userTables = connection.GetSchema("Tables", restrictions);

            for (int i = 0; i < userTables.Rows.Count; i++)
            {
                mdbTblList.Add(userTables.Rows[i][2].ToString());
            }
        }
        catch
        {

        }
        return mdbTblList;
    }

XAML

&lt;ComboBox Grid.ColumnSpan="2" Margin="0,40,0,130" DataContext="{Binding ComboViewModel}"  SelectedItem="{Binding ListTable1}"&gt; &lt;/ComboBox&gt;

【问题讨论】:

  • 只是对您的模型类的评论:模型不包含任何逻辑。此规则的唯一例外是验证。

标签: c# wpf xaml mvvm combobox


【解决方案1】:

让我们专注于视图:为了显示项目将ComboBox.ItemsSource 绑定到对象集合(我想是db 表名)。该集合应位于组合框的 DataContext (ComboViewModel) 中。对于组合框中的SelectedItem 绑定,您需要一个与 ComboViewModel 中集合的泛型类型相同类型的属性 ListTable1(为什么不重命名为 SelectedTable?)。


编辑

public class ComboViewModel : INotifyPropertyChanged {
   private List<string> tables;
   public List<string> Tables {
      get { return tables; }
      set {
         tables = value;
         OnPropertyChanged("Tables");
      }
   }

   private string selectedTable;
   public string SelectedTable {
      get { return selectedTable; }
      set {
            selectedTable = value;
            // react to user selection
        }
   }

   public event PropertyChangedEventHandler PropertyChanged;
   private void RaisePropertyChanged(string propertyName) {
      if (this.PropertyChanged != null)
         this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
   }
}

public class FileDialogViewModel {
    ComboViewModel ComboVM {
        get { /* return ComboViewModel instance */ }
    }

    void UpdateTables(string mdbDir) {
        ComboVM.Tables = TablesList(mdbDir);
    }
}

Xaml

<!-- DataContext has to be the ComboViewModel instance -->
<ComboBox ItemsSource="{Binding Tables}" SelectedItem="{Binding SelectedTable}" />

【讨论】:

  • 嗨,2个表列出了我的程序的具体情况,我的程序基本上基于MDB文件假设获取表列表,但我正在搜索如何触发comboviewmodel并填充组合基于filedialogviewmodel,应该在视图模型中更改什么才能触发它+另外填充组合我如何将文件路径传递给 Tableslist 函数
  • 为了简短起见,我需要在获取文件路径后填充组合框
  • 那么你需要这样的东西:在 ComboViewModel 中一个公共属性 List&lt;string&gt; Tables 及其设置器调用 OnPropertyChanged;在 ComboBox-Tag 绑定 ItemsSource={Binding Tables} (删除当前的 SelectedItem 绑定,这没有意义);在 FileDialogViewModel 中引用您的 ComboViewModel 实例;用户在 FileDialogViewModel 中选择后,将ComboViewModel.Tables 设置为您的TablesList 方法生成的列表。
  • 谢谢你的解释你有任何代码示例吗?
  • 谢谢你的回答,很有帮助,是mvvm的标准吗?可以委托方式完成吗?
【解决方案2】:

我的解决方案是将属性设为公共 ComboViewModel ComboViewModel 静态并在活动文件对话框视图模型上发送表格列表

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多