[英]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.