【发布时间】: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
<ComboBox Grid.ColumnSpan="2" Margin="0,40,0,130" DataContext="{Binding ComboViewModel}" SelectedItem="{Binding ListTable1}"> </ComboBox>
【问题讨论】:
-
只是对您的模型类的评论:模型不包含任何逻辑。此规则的唯一例外是验证。