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.
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.
