Wpf listbox项的xaml绑定值是否为
我正在VS2012中构建一个WPF应用程序,它使用代码优先实体框架、存储库和MVVM模式 我有两个类:EntityType和LicenseType EntityType由以下项指定:Wpf listbox项的xaml绑定值是否为,wpf,xaml,syncfusion,Wpf,Xaml,Syncfusion,我正在VS2012中构建一个WPF应用程序,它使用代码优先实体框架、存储库和MVVM模式 我有两个类:EntityType和LicenseType EntityType由以下项指定: private int _id; private string _name; private string _description; private List<LicenseType> _licenseTypes = new List<LicenseType>
private int _id;
private string _name;
private string _description;
private List<LicenseType> _licenseTypes = new List<LicenseType>();
并且为这些私有字段声明了公共属性
我正在将ObservableCollection绑定到CheckedListBox(Syncfusion产品)
没问题
在它下面,我将另一个CheckedListBox绑定到一个ObservableCollection
再说一次,没问题
我不能做的是根据特定的LicenseType是否在EntityType.LicenseTypes集合中,将LicenseType列表框中CheckListBoxItem的IsSelected属性设置为true(从而“选中”复选框)
以下是我的CheckedListBox的代码:
<syncfusion:CheckListBox x:Name="lstAllLicenseTypes" ItemsSource="{Binding AllLicenseTypes}" DisplayMemberPath="Name" >
<syncfusion:CheckListBox.ItemContainerStyle>
<Style TargetType="syncfusion:CheckListBoxItem">
<Setter Property="IsSelected" Value="{Binding ????}"/>
</Style>
</syncfusion:CheckListBox.ItemContainerStyle>
</syncfusion:CheckListBox>
此处的目的是绑定到属性“CurrentEntityIsAssociatedWithCurrentLicenseType”。这不起作用。我认为它不起作用,因为对于CheckedListBox中的每个项目,属性值都已更新,因此,在最后,它为false,因此没有检查任何项目
另一种方法与上面的方法类似,但我试图手动“检查”列表框项,但是我无法将LicenseType转换回CheckListItem
总的来说,我不擅长的是处理与其他数据集合相关的记录集合,并构建一个界面,允许用户清楚地添加和关联不同的表
任何帮助都会很好
谢谢!
保罗使用转换器。将IsChecked绑定到包装列表的属性(EntityType.LicenseTypes),然后使用返回布尔值的IValueConverter。将CheckedListBoxItem的LicenseType作为转换器参数传递 在EntityType类上实现IPropertyNofity 当EntityType.LicenseTypes列表更改时,使用包装属性上的PropertyNotify,isChecked将更新 Psuedo代码:
public class EntityType : INotifyPropertyChanged
{
...
public List<LicenseType> LicenseTypes
{
get { return _licenseTypes; }
set
{
_licenseTypes = value;
OnNotifyPropertyChanged("LicenseTypes");
}
}
}
public class ListToCheckedConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter ...
{
var list = (List<LicenseType>)value;
var type = (LicenseType)parameter;
return list.Contains(type);
}
...
}
公共类EntityType:INotifyPropertyChanged
{
...
公共列表许可证类型
{
获取{return\u licenseTypes;}
设置
{
_许可证类型=值;
OnNotifyPropertyChanged(“许可证类型”);
}
}
}
用于检查转换器的公共类列表:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数。。。
{
var list=(list)值;
var type=(LicenseType)参数;
返回列表。包含(类型);
}
...
}
XAML:
或者,您可以将IsSelected绑定到CheckedListBoxItem的标题本身,并传入EntityList,但是如果EntityList更新,您将失去自动更新的功能。或者,您可以使用多重绑定并绑定标题和EntityList,如果其中任何一个更新。只是一些想法。我实现了但是,您在第一个解决方案中给出的anges,转换器函数没有启动。下面的代码是:public class ListToCheckedConverter:IValueConverter{public object Convert(object value,Type targetType,object parameter,CultureInfo culture){var list=(observeCollection)value;var Type=(LicenseType)参数;返回列表。包含(类型);}公共对象ConvertBack(对象值、类型targetType、对象参数、文化信息文化){抛出新的NotImplementedException();}}IValueConverter函数Convert未启动。
foreach (object t in lstAllLicenseTypes.Items)
{
bool hasAssociation = _vmEntityTypes.EntityHasAssociationWithThisLicenseType(thisEntity,(LicenseType) t);
if (hasAssociation)
{
_vmEntityTypes.CurrentEntityIsAssociatedWithCurrentLicenseType = true;
}
else
{
_vmEntityTypes.CurrentEntityIsAssociatedWithCurrentLicenseType = false;
}
}
}
public class EntityType : INotifyPropertyChanged
{
...
public List<LicenseType> LicenseTypes
{
get { return _licenseTypes; }
set
{
_licenseTypes = value;
OnNotifyPropertyChanged("LicenseTypes");
}
}
}
public class ListToCheckedConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter ...
{
var list = (List<LicenseType>)value;
var type = (LicenseType)parameter;
return list.Contains(type);
}
...
}
<syncfusion:CheckListBox x:Name="lstAllLicenseTypes" ItemsSource="{Binding AllLicenseTypes}" DisplayMemberPath="Name" >
<syncfusion:CheckListBox.ItemContainerStyle>
<Style TargetType="syncfusion:CheckListBoxItem">
<Setter Property="IsSelected"
Value="{Binding Path=LicenseTypes,
Converter = ListToCheckedConverter,
ConverterParameter = Header}"/>
</Style>
</syncfusion:CheckListBox.ItemContainerStyle>
</syncfusion:CheckListBox>