Silverlight ComboBox过滤器 - 全部显示

时间:2012-10-25 17:30:50

标签: silverlight xaml silverlight-5.0

在带有RIA服务的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>

enter image description here

想要在ComboBox

中加载数据后手动添加Show All in code

EDIT 感谢马丁,我得到了这样的工作:

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;
        }

1 个答案:

答案 0 :(得分:0)

在过去,我通过创建一个代理类来实现这一点,类似于带有display,key和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的额外项目而不是试图直接绑定到列表。

希望有所帮助。