0
votes

I resolved the issue of not being able to filter comboboxes based on various criteria using CollectionViewSource and ICollectionView, but now I am having another issue.

When I set these filters, it automatically selects the first item in the filter, when I don't want anything to be selected, I simply want the items there to be able to be selected.

Is there a way to set the filters without them automatically selecting the first items?

Even when I set the property it is bound to, RoleStr(well the public accessible property, technically _roleStr is the private one it uses) back to "", the Combobox starts out with the first item in the FilteredView showing.

Combobox still shows a value in it, when I simply want it to show up as blank.

XAML:

<ComboBox  x:Name="empRoleCB" Height="20" Width="175" HorizontalAlignment="Left"   VerticalAlignment="Top" Margin="0,5" IsEnabled="{Binding ElementName=empDeptCB, Path=Text.Length, Mode=OneWay}"   ItemsSource="{Binding Path=MyRoleFilter}" SelectedItem="{Binding RoleStr}" SelectionChanged="empDeptCB_SelectionChanged" Loaded="empRoleCB_Loaded"/>

ViewModel:

public partial class EmployeeMenu : Window
{
    EmployeeMenuVM empVM = new EmployeeMenuVM();
    public EmployeeMenu()
    {

        DataContext = empVM;
        empVM.MyRoleFilter = new CollectionViewSource { Source = empVM.Role }.View;

        InitializeComponent();
    }
    private void empRoleCB_Loaded(object sender, RoutedEventArgs e)
    {
        if(loggedUser[0].Role == (int)Roles.SrMgr)
        {
            empVM.MyRoleFilter.Filter = a => { return (string)a == Roles.Mgr.ToString() || (string)a == Roles.TeamLead.ToString() || (string)a == Roles.User.ToString(); };
        }
        else if(loggedUser[0].Role == (int)Roles.Mgr)
        {
            empVM.MyRoleFilter.Filter = a => { return (string)a == Roles.TeamLead.ToString() || (string)a == Roles.User.ToString(); };
        }
       else if (loggedUser[0].Role == (int)Roles.TeamLead)
        {
            empVM.MyRoleFilter.Filter = a => { return  (string)a == Roles.User.ToString(); };
        }

        empVM.RoleStr = "";
    }

View:

private ObservableCollection<string> _role = new ObservableCollection<string>(Enum.GetNames(typeof(Global.Roles)));
private string _roleStr;
public IEnumerable<string> Role { get => _role; }
public ICollectionView MyRoleFilter { get; set; }
public string RoleStr
{
     get => _roleStr;
     set => SetProperty(ref _roleStr, value);
}

UPDATE SOLVED:

I had to manually set the Combobox.Text property to empty after I ran the filters and this resolved the issue.

1
Please provide a MCVE when asking a question on SO. - mm8
I am pretty sure if you disable the combobox before you change the filter, and then reenable after you set the filter, it should not do a selection. - user4843530
@AgapwIesu No this is not true, the combobox IS disabled and it still ends up having a value showing in it. - MattE
Ok. Sorry. Going from vague recollection of having this very problem a few years ago. How about having a value in the selections that is a "please select a value" kind of option? - user4843530

1 Answers

0
votes

Remove SelectedItem. Add IsSynchronizedWithCurrentItem="True".

ICollectionView handles the current item internally. You can query it through the ICollectionView.CurrentItem property.