[英]Silverlight ComboBox Filter - Show All
在带有RIA Services的Silverlight5中,使用DomainDataSources。
我有一个绑定到DataGrid的过滤器ComboBox。
问题:如何在组合框顶部实现“全选”-当前已通过在数据库中放置ID = 0的行并在过滤器中使用IgnoredValue =“ 0”来实现
<riaControls:DomainDataSource.FilterDescriptors>
<riaControls:FilterDescriptor PropertyPath="AccountTypeID" Operator="IsEqualTo" IgnoredValue="0" Value="{Binding Path=SelectedItem.AccountTypeID,
ElementName=AccountTypeComboBox, FallbackValue=0}" />
</riaControls:DomainDataSource.FilterDescriptors>
</riaControls:DomainDataSource>
<riaControls:DomainDataSource x:Name="AccountTypeDataSource" AutoLoad="True" QueryName="GetAccountTypes" PageSize="15" LoadSize="30">
<riaControls:DomainDataSource.DomainContext>
<domain:xxxDomainContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
想要在将数据加载到ComboBox中之后手动添加“显示全部代码”
编辑感谢下面的马丁,我像这样工作:
private xxxDomainContext xxxDomainContext;
public MainPage()
{
InitializeComponent();
// Load up the AccountType ComboBox - instead of doing it in XAML
// then add in the Select All via proxy class above
xxxDomainContext = new xxxDomainContext();
EntityQuery<AccountType> query = xxxDomainContext.GetAccountTypesQuery();
LoadOperation loadOperation = xxxDomainContext.Load<AccountType>(query);
// everything is async so need a callback, otherwise will get an empty collection when trying to iterate over it here
loadOperation.Completed += AccountTypeLoadOperationCompleted;
和
private void AccountTypeLoadOperationCompleted(object sender, System.EventArgs e)
{
// create new proxy class
var listOfSelectableObjects = new List<SelectableObject<int>>();
var selectAll = new SelectableObject<int> { Display = "Select All", KeyValue = 0};
listOfSelectableObjects.Add(selectAll);
// load values into new list
foreach (var accountType in xxxDomainContext.AccountTypes)
{
var so = new SelectableObject<int>();
so.Display = accountType.Description;
so.KeyValue = accountType.AccountTypeID;
listOfSelectableObjects.Add(so);
}
AccountTypeComboBox.ItemsSource = listOfSelectableObjects;
// Set index to 0 otherwise a blank item will appear at the top and be selected
AccountTypeComboBox.SelectedIndex = 0;
}
过去,我是通过创建一个代理类来实现此目的的,例如带有显示,键和isDefault的SelectableValue,如下所示:
public class SelectableObject<K>
{
public string Display { get; set; }
public K KeyValue { get;set; }
public bool IsDefaultSelection { get; set; }
}
这意味着我可以将例如要选择的项目列表转换为List<SelectableObject<int>>
,并可以选择向该列表添加一个额外的项目,即Display="Select All"
和IsDefault=true
而不是尝试直接绑定到列表。
希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.