lundi 28 mars 2016

Update Entity Framework navigation property ICollection items

I'm using Entity Framework 6 in Visual Studio 2015 to build an MVVM Light WPF app. I need to bind a navigation property ICollection to some CheckBox controls. An Employee can have 0 to at most 3 EmployeeStatus entities, with employeeID serving as the key on the EmployeeStatus entity; EmployeeStatus in turn has a foreign key on the EmployeeStatusDescription table of employeeStatusID. The EmployeeStatusDescription provides the description of the status code (such as "Archived", "Inactive", "Leave of Absence"). Each EmployeeStatus corresponds to one EmployeeStatusDescription.

EmployeeStatus is defined this way in the Employee class:

public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; }

EmployeeStatusDescription is defined in the EmployeeStatus class as:

public virtual EmployeeStatusDescription EmployeeStatusDescription { get; set; }

I'd like to show 3 CheckBox controls and bind each to the value from the EmployeeStatus ICollection values. For example, if an employee does not have status "Inactive" and the user checks that, I need to add that to the EmployeeStatus collection; if the user unchecks that item, I'd like to have it removed from the EmployeeStatus collection.

I've created the following StackPanel to hold the checkboxes; they're bound to properties on my view model:

<StackPanel Grid.Row="12"
            Grid.Column="1"
            Orientation="Vertical">
    <StackPanel Orientation="Horizontal">
        <TextBlock HorizontalAlignment="Left"
                   VerticalAlignment="Top"
                   Text="Inactive" />
        <CheckBox IsChecked="{Binding IsSelectedEmployeeInActive}" />
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <TextBlock HorizontalAlignment="Left"
                   VerticalAlignment="Top"
                   Text="Leave of Absence" />
        <CheckBox IsChecked="{Binding IsSelectedEmployeeLoa}" />
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <TextBlock HorizontalAlignment="Left"
                   VerticalAlignment="Top"
                   Text="Archived" />
        <CheckBox IsChecked="{Binding IsSelectedEmployeeArchived}" />
    </StackPanel>                                    
</StackPanel>

Here's an example property bound to one of the CheckBox controls' IsChecked dependency property:

private bool _isSelectedEmployeeInActive;

public bool IsSelectedEmployeeInActive
{
    get { return _isSelectedEmployeeInActive; }
    set
    {
        if (_isSelectedEmployeeInActive == value) return;

        _isSelectedEmployeeInActive = value;
        RaisePropertyChanged(() => IsSelectedEmployeeInActive);
    }
}   

I'm doing a search to get the entity collection:

var query = (from e in Context.Employees
             .Include("EmployeeStatus.EmployeeStatusDescription")
             .Where(comparison)
             select e);

SearchResults = new ObservableCollection<Employee>(query);




Aucun commentaire:

Enregistrer un commentaire