后台代码破坏数据绑定的解决方法
<dxe:ComboBoxEdit Width=" 150" Name="cbbUnitType" SelectedIndexChanged="cbbUnitType_SelectedIndexChanged"></dxe:ComboBoxEdit>
<ListBox Name="lbCapability" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Capability_Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
界面一个ListBox,一个ComboBox,实现下拉ComboBox实现ListBox主从数据联动
数据绑定加载主数据的ComboBox
List<FactoryVSMTMDL> listUnitTypes = new List<FactoryVSMTMDL>();
listUnitType=基于业务逻辑获取……
listUnitTypes.AddRange(listUnitType);
cbbUnitType.ItemsSource = listUnitTypes;
cbbUnitType.DisplayMember = "Unit_Type_Name";
cbbUnitType.ValueMember = "Unit_Type_ID";
cbbUnitType.SelectedIndex = 0;
private void cbbUnitType_SelectedIndexChanged(object sender, RoutedEventArgs e)
{
FactoryVSMTMDL UnitType = (FactoryVSMTMDL)cbbUnitType.SelectedItem;
if (UnitType.Unit_Type_ID != 0 && UnitType.Unit_Type_ID != null)
{
if (!factoryDAL.GetCapabilityByUnitType(UnitType.Unit_Type_ID, ref listCapability, ref strErrMsg))
{
MessageBox.Show(strErrMsg);
return;
}
ListCapabilities.Clear();
ListCapabilities.AddRange(listCapability);
//后台逻辑常常会解除绑定,因此常常需要判断是否重新绑定
DependencyProperty dp = ListBox.ItemsSourceProperty;
if (!BindingOperations.IsDataBound(lbCapability, dp))
{//判断是否该控件存在绑定
Binding bingding = new Binding("Capability_Name");
BindingOperations.SetBinding(lbCapability, dp, bingding);
}
lbCapability.ItemsSource = ListCapabilities;
}
}