执行验证时在应用程序启动时进行验证,并刷新列表视图

时间:2018-10-16 08:36:32

标签: c# wpf validation listview mvvm

我正在开发一个非常简单的应用程序(我正在学习C#WPF MVVM :)。我在验证时遇到2个问题。

验证: 当“文本框”为空时,应该开始验证。

第一个问题是,在复制开始时无法进行验证。我必须在文本框中写一些内容,然后将其删除。然后验证有效。我想在应用程序运行后立即开始验证。

enter image description here 开始时显示我的应用程序的视图(红色边框应该用空白文本框包围)。

第二个问题是,执行验证时,我与员工的列表没有恢复为原始形式。

enter image description here Listview不会恢复为原始形式。

你能帮我吗?在下面,我放置了验证器xaml模型的代码。如果您需要完整的代码,请告诉我:)

员工。 cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows.Threading;

namespace OneWayTwoWayBinding
{
    public class Employee : INotifyPropertyChanged
    {
        private string employeeName;
        private int? employeeID;
        private int? employeeSalary;
        private string employeeDesigner;
        private string employeeEmailID;
        private Employee selectedEmployee;
        private ICollectionView filteredCollection;
        private string dynamicSearchEmployeeName;
        private int? dynamicSearchEmployeeID;
        private int? dynamicSearchEmployeeSalary;
        private string dynamicSearchEmployeeDesigner;
        private string dynamicSearchEmployeeEmailID;
        private int isSelected;
        private string modeOfExecuting;
        private string rememberValueEmployeeName;
        private int? rememberValueEmployeeID;
        private int? rememberValueEmployeeSalary;
        private string rememberValueEmployeeDesigner;
        private string rememberValueEmployeeEmailID;
        private ObservableCollection<Employee> employees;


        public string EmployeeName
        {
            get
            {
                if (FilteredCollection != null)
                    FilteredCollection.Filter = x => (String.IsNullOrEmpty(employeeName) || ((Employee)x).EmployeeName.Contains(employeeName));
                return employeeName;
            }
            set
            {
                employeeName = value;
                OnPropertyChanged("EmployeeName");
                if (FilteredCollection != null)
                    FilteredCollection.Filter = x => (String.IsNullOrEmpty(employeeName) || ((Employee)x).EmployeeName.Contains(employeeName));
            }
        }
        public int? EmployeeID
        {
            get
            {
                if (FilteredCollection != null)
                    FilteredCollection.Filter = x => ((employeeID == null) || ((Employee)x).EmployeeID == employeeID);
                return employeeID;
            }
            set
            {

                employeeID = value;
                OnPropertyChanged("EmployeeID");
                if (FilteredCollection != null)
                    FilteredCollection.Filter = x => ((employeeID == null) || ((Employee)x).EmployeeID == employeeID);
            }
        }
        public int? EmployeeSalary
        {
            get
            {
                if (FilteredCollection != null)
                    FilteredCollection.Filter = x => ((employeeSalary == null) || ((Employee)x).EmployeeSalary == employeeSalary);
                return employeeSalary;
            }
            set
            {
                employeeSalary = value;
                OnPropertyChanged("EmployeeSalary");
                if (FilteredCollection != null)
                    FilteredCollection.Filter = x => ((employeeSalary == null) || ((Employee)x).EmployeeSalary == employeeSalary);
            }
        }
        public string EmployeeDesigner
        {
            get
            {
                //Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(employeeDesigner)));
                if (FilteredCollection != null)
                    FilteredCollection.Filter = x => (String.IsNullOrEmpty(employeeDesigner) || ((Employee)x).EmployeeDesigner.Contains(employeeDesigner));
                return employeeDesigner;
            }
            set
            {
                employeeDesigner = value;
                OnPropertyChanged("EmployeeDesigner");
                if (FilteredCollection != null)
                FilteredCollection.Filter = x => (String.IsNullOrEmpty(employeeDesigner) || ((Employee)x).EmployeeDesigner.Contains(employeeDesigner));
            }
        }
        public string EmployeeEmailID
        {
            get
            {
                if (FilteredCollection != null)
                    FilteredCollection.Filter = x => (String.IsNullOrEmpty(employeeEmailID) || ((Employee)x).EmployeeEmailID.Contains(employeeEmailID));
                return employeeEmailID;
            }
            set
            {
                employeeEmailID = value;
                OnPropertyChanged("EmployeeEmailID");
                if (FilteredCollection != null)
                    FilteredCollection.Filter = x => (String.IsNullOrEmpty(employeeEmailID) || ((Employee)x).EmployeeEmailID.Contains(employeeEmailID));
            }
        }
        public ObservableCollection<Employee> Employees
        {
            get
            {
                return employees;
            }
            set
            {
                employees = value;
                OnPropertyChanged("Employees");
            }
        }

        public Employee SelectedEmployee
        {
            get
            {
                if (selectedEmployee == null)
                {
                    ModeOfExecuting = "Searching / Adding Mode";
                }
                    //Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(selectedEmployee.SelectedEmployee.ToString())));
                    return selectedEmployee;
            }
            set
            {
                selectedEmployee = value;

                if (selectedEmployee == null)
                {
                    ModeOfExecuting = "Searching / Adding Mode";
                    OnPropertyChanged("SelectedEmployee");
                    OnPropertyChanged("ModeOfExecuting");
                }

                if (selectedEmployee != null)
                {
                    if(selectedEmployee.EmployeeName != string.Empty)
                    {
                        RememberValueEmployeeName = selectedEmployee.EmployeeName;
                        DynamicSearchEmployeeName = RememberValueEmployeeName;

                    }
                    if (selectedEmployee.EmployeeID != null)
                    {
                        RememberValueEmployeeID = selectedEmployee.EmployeeID;
                        DynamicSearchEmployeeID = RememberValueEmployeeID;
                    }
                    if (selectedEmployee.EmployeeSalary != null)
                    {
                        RememberValueEmployeeSalary = selectedEmployee.EmployeeSalary;
                        DynamicSearchEmployeeSalary = RememberValueEmployeeSalary;
                    }
                    if (selectedEmployee.EmployeeDesigner != string.Empty)
                    {
                        RememberValueEmployeeDesigner = selectedEmployee.EmployeeDesigner;
                        DynamicSearchEmployeeDesigner = RememberValueEmployeeDesigner;
                    }
                    if (selectedEmployee.EmployeeEmailID != string.Empty)
                    {
                        RememberValueEmployeeEmailID = selectedEmployee.EmployeeEmailID;
                        DynamicSearchEmployeeEmailID = RememberValueEmployeeEmailID;
                    }

                    ModeOfExecuting = "Editing Mode";

                    OnPropertyChanged("SelectedEmployee");
                    OnPropertyChanged("ModeOfExecuting");
                    OnPropertyChanged("RememberValueEmployeeName");
                    OnPropertyChanged("DynamicSearchEmployeeName");
                    OnPropertyChanged("RememberValueEmployeeID");
                    OnPropertyChanged("DynamicSearchEmployeeID");
                    OnPropertyChanged("RememberValueEmployeeSalary");
                    OnPropertyChanged("DynamicSearchEmployeeSalary");
                    OnPropertyChanged("RememberValueEmployeeDesigner");
                    OnPropertyChanged("RememberValueEmployeeDesigner");
                    OnPropertyChanged("RememberValueEmployeeEmailID");
                    OnPropertyChanged("DynamicSearchEmployeeEmailID");
                }

                selectedEmployee = value;
                OnPropertyChanged("SelectedEmployee");
            }
        }

        public string DynamicSearchEmployeeName
        {
            get
            {
                if (SelectedEmployee == null)
                {
                    //Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("null")));
                    EmployeeName = dynamicSearchEmployeeName;
                    //RememberValue = dynamicSearchEmployeeName;
                }

                return dynamicSearchEmployeeName;
            }
            set
            {
                dynamicSearchEmployeeName = value;
                OnPropertyChanged("DynamicSearchEmployeeName");
            }
        }
        public int? DynamicSearchEmployeeID
        {
            get
            {
                if (SelectedEmployee == null)
                {
                    EmployeeID = dynamicSearchEmployeeID;
                }
                return dynamicSearchEmployeeID;
            }
            set
            {
                dynamicSearchEmployeeID = value;
                OnPropertyChanged("DynamicSearchEmployeeID");
            }
        }
        public int? DynamicSearchEmployeeSalary
        {
            get
            {
                if (SelectedEmployee == null)
                {
                    EmployeeSalary = dynamicSearchEmployeeSalary;
                }

                return dynamicSearchEmployeeSalary;
            }
            set
            {
                dynamicSearchEmployeeSalary = value;
                OnPropertyChanged("DynamicSearchEmployeeSalary");
            }
        }
        public string DynamicSearchEmployeeDesigner
        {
            get
            {
                if (SelectedEmployee == null)
                {
                    EmployeeDesigner = dynamicSearchEmployeeDesigner;
                }
                return dynamicSearchEmployeeDesigner;
            }
            set
            {
                dynamicSearchEmployeeDesigner = value;
                OnPropertyChanged("DynamicSearchEmployeeDesigner");
            }
        }
        public string DynamicSearchEmployeeEmailID
        {
            get
            {
                if (SelectedEmployee == null)
                {
                    EmployeeEmailID = dynamicSearchEmployeeEmailID;
                }
                return dynamicSearchEmployeeEmailID;
            }
            set
            {
                dynamicSearchEmployeeEmailID = value;
                OnPropertyChanged("DynamicSearchEmployeeEmailID");
            }

        }
        public ICollectionView FilteredCollection
        {
            get
            {
                return filteredCollection;
            }
            set
            {
                filteredCollection = value;
                OnPropertyChanged("FilteredCollection");
            }
        }

        public int IsSelected
        {
            get
            {
                //Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(changedPathBinding.ToString())));
                return isSelected;
            }
            set
            {
                isSelected = value;
                OnPropertyChanged("IsSelected");
                //SelectedEmployee.EmployeeName
            }
        }

        public string ModeOfExecuting
        {
            get
            {
                //Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(modeOfExecuting.ToString())));
                return modeOfExecuting;
            }
            set
            {
                modeOfExecuting = value;
                OnPropertyChanged("ModeOfExecuting");
            }
        }
        public string RememberValueEmployeeName
        {
            get
            {
                //Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(changedPathBinding.ToString())));
                return rememberValueEmployeeName;
            }
            set
            {
                rememberValueEmployeeName = value;
                OnPropertyChanged("RememberValueEmployeeName");
            }
        }
        public int? RememberValueEmployeeID
        {
            get
            {
                //Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(changedPathBinding.ToString())));
                return rememberValueEmployeeID;
            }
            set
            {
                rememberValueEmployeeID = value;
                OnPropertyChanged("RememberValueEmployeeID");
            }
        }
        public int? RememberValueEmployeeSalary
        {
            get
            {
                //Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(changedPathBinding.ToString())));
                return rememberValueEmployeeSalary;
            }
            set
            {
                rememberValueEmployeeSalary = value;
                OnPropertyChanged("RememberValueEmployeeSalary");
            }
        }
        public string RememberValueEmployeeDesigner
        {
            get
            {
                //Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(changedPathBinding.ToString())));
                return rememberValueEmployeeDesigner;
            }
            set
            {
                rememberValueEmployeeDesigner = value;
                OnPropertyChanged("RememberValueEmployeeDesigner");
            }
        }
        public string RememberValueEmployeeEmailID
        {
            get
            {
                //Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(changedPathBinding.ToString())));
                return rememberValueEmployeeEmailID;
            }
            set
            {
                rememberValueEmployeeEmailID = value;
                OnPropertyChanged("RememberValueEmployeeEmailID");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged = null;
        virtual protected void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

Validators.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace OneWayTwoWayBinding
{
    public class OnlyIntOrNullRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            int result = 0;
            bool canConvert = int.TryParse(value as string, out result);
            if(string.IsNullOrEmpty(value.ToString()))
            {
                canConvert = true;
            }
            return new ValidationResult(canConvert, "Not a number");
        }
    }
    public class RequiredValuesToAddRule : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            bool RequiredValue = false;
            if ((string)value == string.Empty)
            {
                //MessageBox.Show("EMPTY");
                RequiredValue = false;
                //MessageBox.Show(value.ToString());
            }
            else if ((string)value != string.Empty)
            {
                //MessageBox.Show("NOTEMPTY");
                RequiredValue = true;
                //MessageBox.Show(value.ToString());
            }
            return new ValidationResult(RequiredValue, "Value cannot be null");
        }

    }
}

MainWindow.xaml

<Window x:Class="OneWayTwoWayBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:OneWayTwoWayBinding"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:ConverterStringFiltering x:Key="ConverterStringFiltering" />
        <local:ConverterButton x:Key="ConverterButton" />
        <local:ConverterIntFiltering x:Key="ConverterIntFiltering" />
    </Window.Resources>
    <Grid Margin="0,0,0,20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ListView Name="EmployeeListView" HorizontalAlignment="Left" Height="160" Margin="0,259,0,0" VerticalAlignment="Top" Width="792" ItemsSource="{Binding FilteredCollection}" SelectedItem="{Binding SelectedEmployee, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedIndex="{Binding IsSelected}" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="EmployeeName" Width="150" DisplayMemberBinding="{Binding EmployeeName}" />
                    <GridViewColumn Header="EmployeeID" Width="150" DisplayMemberBinding="{Binding EmployeeID}" />
                    <GridViewColumn Header="EmployeeSalary" Width="150" DisplayMemberBinding="{Binding EmployeeSalary}" />
                    <GridViewColumn Header="EmployeeDesigner" Width="150" DisplayMemberBinding="{Binding EmployeeDesigner}" />
                    <GridViewColumn Header="EmployeeEmailID" Width="150" DisplayMemberBinding="{Binding EmployeeEmailID}" />
                </GridView>
            </ListView.View>
        </ListView>
        <Label Content="Employee Name" HorizontalAlignment="Left" Margin="0,52,0,0" VerticalAlignment="Top" Width="151" Height="23"/>

        <TextBox Name ="TextboxEmployeeName" HorizontalAlignment="Left" Height="23" Margin="165,52,0,0" VerticalAlignment="Top" Width="522" >
            <TextBox.Text>
                <MultiBinding Converter="{StaticResource ConverterStringFiltering}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnExceptions="True" ValidatesOnDataErrors="True">
                    <MultiBinding.ValidationRules>
                        <local:RequiredValuesToAddRule ValidationStep="RawProposedValue"/>
                    </MultiBinding.ValidationRules>
                    <Binding Path="SelectedEmployee.EmployeeName" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"/>
                    <Binding Path="DynamicSearchEmployeeName"  Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
                </MultiBinding>
            </TextBox.Text>
        </TextBox>

        <Label Content="EmplyeeID" HorizontalAlignment="Left" Margin="0,91,0,0" VerticalAlignment="Top" Width="145" Height="23"/>

        <TextBox Name ="TextboxEmployeeID" HorizontalAlignment="Left" Height="23" Margin="165,91,0,0"  VerticalAlignment="Top" Width="522" >
            <TextBox.Text>
                <MultiBinding Converter="{StaticResource ConverterIntFiltering}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                    <MultiBinding.ValidationRules>
                        <local:RequiredValuesToAddRule/>
                    </MultiBinding.ValidationRules>
                    <Binding Path="SelectedEmployee.EmployeeID" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"/>
                    <Binding Path="DynamicSearchEmployeeID"  Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
                </MultiBinding>
            </TextBox.Text>
        </TextBox>


        <Label Content="EmpoyeeSalary" HorizontalAlignment="Left" Margin="0,131,0,0" VerticalAlignment="Top" Width="151" Height="23"/>

        <TextBox Name="TextboxEmployeeSalary" HorizontalAlignment="Left" Height="23" Margin="165,131,0,0" VerticalAlignment="Top" Width="522" >
            <TextBox.Text>
                <MultiBinding Converter="{StaticResource ConverterIntFiltering}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                    <MultiBinding.ValidationRules>
                        <local:OnlyIntOrNullRule/>
                    </MultiBinding.ValidationRules>
                    <Binding Path="SelectedEmployee.EmployeeSalary" Mode="OneWay" UpdateSourceTrigger="PropertyChanged" />
                    <Binding Path="DynamicSearchEmployeeSalary"  Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
                </MultiBinding>
            </TextBox.Text>
        </TextBox>

        <Label Content="EmployeeDesigner" HorizontalAlignment="Left" Margin="0,176,0,0" VerticalAlignment="Top" Width="151" Height="23"/>

        <TextBox Name ="TextboxEmployeeDesigner" HorizontalAlignment="Left" Height="23" Margin="165,176,0,0" VerticalAlignment="Top" Width="522" >
            <TextBox.Text>
                <MultiBinding Converter="{StaticResource ConverterStringFiltering}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                    <Binding Path="SelectedEmployee.EmployeeDesigner" Mode="OneWay" UpdateSourceTrigger="PropertyChanged" />
                    <Binding Path="DynamicSearchEmployeeDesigner"  Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
                </MultiBinding>
            </TextBox.Text>
        </TextBox>

        <Label Content="EmployeeEmailID" HorizontalAlignment="Left" Margin="0,221,0,0" VerticalAlignment="Top" Width="151" Height="23"/>
        <TextBox Name="TextboxEmployeeEmailID" HorizontalAlignment="Left" Height="23" Margin="165,221,0,0" VerticalAlignment="Top" Width="522" >

            <TextBox.Text>
                <MultiBinding Converter="{StaticResource ConverterStringFiltering}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                    <Binding Path="SelectedEmployee.EmployeeEmailID" Mode="OneWay" UpdateSourceTrigger="PropertyChanged" />
                    <Binding Path="DynamicSearchEmployeeEmailID"  Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
                </MultiBinding>
            </TextBox.Text>
        </TextBox>

        <Button Content="Update" HorizontalAlignment="Left" Margin="692,76,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.017,0.456" Command="{Binding UpdateCommand}" >
            <Button.CommandParameter>
                <MultiBinding Converter="{StaticResource ConverterButton}" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
                    <Binding ElementName="TextboxEmployeeName" Path="Text"/>
                    <Binding ElementName="TextboxEmployeeID" Path="Text"/>
                    <Binding ElementName="TextboxEmployeeSalary" Path="Text"/>
                    <Binding ElementName="TextboxEmployeeDesigner" Path="Text"/>
                    <Binding ElementName="TextboxEmployeeEmailID" Path="Text"/>
                </MultiBinding>
            </Button.CommandParameter>
        </Button>
        <TextBlock HorizontalAlignment="Left" Margin="15,10,0,0" TextWrapping="Wrap" Text="{Binding ModeOfExecuting, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" VerticalAlignment="Top" Width="390" Height="37" FontSize="20"/>
        <Button Content="Clear" HorizontalAlignment="Left" Margin="692,116,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.017,0.456" Command="{Binding ClearCommand}" >
            <Button.CommandParameter>
                <MultiBinding Converter="{StaticResource ConverterButton}" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
                    <Binding ElementName="TextboxEmployeeName" Path="Text"/>
                    <Binding ElementName="TextboxEmployeeID" Path="Text"/>
                    <Binding ElementName="TextboxEmployeeSalary" Path="Text"/>
                    <Binding ElementName="TextboxEmployeeDesigner" Path="Text"/>
                    <Binding ElementName="TextboxEmployeeEmailID" Path="Text"/>
                </MultiBinding>
            </Button.CommandParameter>
        </Button>
        <Button Content="Add" HorizontalAlignment="Left" Margin="692,161,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.017,0.456" Command="{Binding AddCommand}" >
            <Button.CommandParameter>
                <MultiBinding Converter="{StaticResource ConverterButton}" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
                    <Binding ElementName="TextboxEmployeeName" Path="Text"/>
                    <Binding ElementName="TextboxEmployeeID" Path="Text"/>
                    <Binding ElementName="TextboxEmployeeSalary" Path="Text"/>
                    <Binding ElementName="TextboxEmployeeDesigner" Path="Text"/>
                    <Binding ElementName="TextboxEmployeeEmailID" Path="Text"/>
                </MultiBinding>
            </Button.CommandParameter>
        </Button>
        <Button Content="Remove" HorizontalAlignment="Left" Margin="692,206,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.017,0.456" Command="{Binding RemoveCommand}" />
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

关于验证无法立即生效,我发现了一个类似的问题:

WPF Validation not firing when first load

如果其解决方案不起作用,建议对您所有的文本绑定属性引发某种属性更改事件,如下所示:

OnPropertyChanged("SelectedEmployee.EmployeeName");

在程序的起始位置,构造函数等“ InitializeComponents();”之后的某个位置部分。

关于第二个问题;

我找到了一个有关此案例的不错的教程:

How to Navigate, Group, Sort and Filter Data in WPF

简而言之,为了清除过滤器,您必须使用“ .Refresh”,如下所示:

FilteredCollection.Refresh();

根据我的建议,您可以将其放置在任何位置

public string EmployeeName
{
    get
    {
        if (FilteredCollection != null)
            FilteredCollection.Filter = x => (String.IsNullOrEmpty(employeeName) || ((Employee)x).EmployeeName.Contains(employeeName));
        return employeeName;
    }
    set
    {
        employeeName = value;
        OnPropertyChanged("EmployeeName");
        if (FilteredCollection != null)
            FilteredCollection.Filter = x => (String.IsNullOrEmpty(employeeName) || ((Employee)x).EmployeeName.Contains(employeeName));


        // The changes
        if (String.IsNullOrEmpty(value))
            ClearFilter();
    }
}

// The changes
private void ClearFilter()
{
    FilteredCollection.Refresh();
}