一、前言

在界面编程中,我们常常会遇到具有依赖性质的ComboBox框,比如最常见的:

省/直辖市 => 地级市/区 => 区/街道

今天就说一下在WPF的MVVM模式中如何实现该功能

二、范例

      假设有一段原始数据,名为:TotalData

其包含四个可以过滤的属性:AType、BType、CType、DType

与其对应的有四个ComboBox:

1. SelectedItem绑定:ATypeSelected   ItemsSource绑定为:ATypes

2. SelectedItem绑定:BTypeSelected   ItemsSource绑定为:BTypes

3. SelectedItem绑定:CTypeSelected   ItemsSource绑定为:CTypes

4. SelectedItem绑定:DTypeSelected   ItemsSource绑定为:DTypes

class Temp
{
private string _ATypeSelected;
public string ATypeSelected
{
get
{
return _ATypeSelected;
}
set
{
if (SetProperty(ref _ATypeSelected, value, () => ATypeSelected))
{
BTypes.Clear();
var BTempTypes = new List<string>(); if (ATypeSelected != "全部A类型" && ATypeSelected != null)
{
BTempTypes.AddRange(TotalData.Where(x => x.AType == ATypeSelected).Select(item => item.BType));
}
else
{
BTempTypes.AddRange(TotalData.Select(item => item.BType));
} BTypes.Add("全部B类型");
foreach (var item in BTempTypes.Distinct())
{
BTypes.Add(item);
}
BTypeSelected = BTypes[];
}
}
} private string _BTypeSelected;
public string BTypeSelected
{
get
{
return _BTypeSelected;
}
set
{
if (SetProperty(ref _BTypeSelected, value, () => BTypeSelected))
{
CTypes.Clear();
var CTempTypes = new List<string>(); if (ATypeSelected != "全部A类型" && ATypeSelected != null)
{
if (BTypeSelected != "全部B类型" && BTypeSelected != null)
{
CTempTypes.AddRange(from item in TotalData where item.AType == ATypeSelected && item.BType == BTypeSelected select item.CType);
}
else if (BTypeSelected == "全部B类型")
{
CTempTypes.AddRange(from item in TotalData where item.AType == ATypeSelected select item.CType);
}
}
else
{
if (BTypeSelected != "全部B类型" && BTypeSelected != null)
{
CTempTypes.AddRange(from item in TotalData where item.BType == BTypeSelected select item.CType);
}
else if (BTypeSelected == "全部B类型")
{
CTempTypes.AddRange(TotalData.Select(item => item.CType));
}
}
CTypes.Add("全部C类型");
foreach (var item in CTempTypes.Distinct())
{
CTypes.Add(item);
}
CTypeSelected = CTypes[];
}
}
} private string _CTypeSelected;
public string CTypeSelected
{
get
{
return _CTypeSelected;
}
set
{
if (SetProperty(ref _CTypeSelected, value, () => CTypeSelected))
{
DTypes.Clear();
var DTempTypes = new List<string>(); if (ATypeSelected != "全部A类型" && ATypeSelected != null)
{
if (BTypeSelected != "全部B类型" && BTypeSelected != null)
{
if (CTypeSelected != null && CTypeSelected != "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.BType == BTypeSelected && item.CType == CTypeSelected && item.AType == ATypeSelected select item.DType);
}
else if (CTypeSelected == "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.BType == BTypeSelected && item.AType == ATypeSelected select item.DType);
} }
else if (BTypeSelected == "全部B类型")
{
if (CTypeSelected != null && CTypeSelected != "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.CType == CTypeSelected && item.AType == ATypeSelected select item.DType);
}
else if (CTypeSelected == "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.AType == ATypeSelected select item.DType);
}
}
}
else
{
if (BTypeSelected != "全部B类型" && BTypeSelected != null)
{
if (CTypeSelected != null && CTypeSelected != "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.BType== BTypeSelected && item.CType == CTypeSelected select item.DType);
}
else if (CTypeSelected == "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.BType == BTypeSelected select item.DType);
} }
else if (BTypeSelected == "全部B类型")
{
if (CTypeSelected != null && CTypeSelected != "全部C类型")
{
DTempTypes.AddRange(from item in TotalData where item.CType == CTypeSelected select item.DType);
}
else if (CTypeSelected == "全部C类型")
{
DTempTypes.AddRange(TotalData.Select(item => item.DType));
}
}
} DTypes.Add("全部D类型");
foreach (var item in DTempTypes.Distinct())
{
DTypes.Add(item);
}
DTypeSelected = DTypes[];
}
}
} private string _DTypeSelected; public string DTypeSelected
{
get { return _DTypeSelected; }
set
{
SetProperty(ref _DTypeSelected, value, () => DTypeSelected);

//在此处可以根据上述的ATypeSelected、BTypeSelected、CTypeSelected、DTypeSelected 对 TotalData 进行过滤从而得到想要显示在界面上的数据列表 }
} }
05-26 14:10