MVC WPF DataContext for two UserControls

201 views Asked by At

I'm creating a WPF Application.

I have a View which implements two different User-Controls. The Data Context for each Control needs to be the same, because when I change values in one control it should effect the other one. As soon as I select a date in my view the DataContext (which is a List of information) should be set to each control. I already tried Binding the List to the Control. What kinda worked. The Listbox showed that there were elements but the content was not visible.

<ListView x:Name="ListViewEmployees" ItemsSource="{Binding Employees}">
    <ListView.Items>
        <Label x:Name="EmployeeId" Content="{Binding Path=EmployeeId}" Visibility="Collapsed" />
        <Label x:Name="ShortForm" Content="{Binding Path=ShortForm}" Width="40"/>
        <Label x:Name="Degree" Content="{Binding Path=Degree}" Width="90"/>
        <Label x:Name="WorkingHours" Content="{Binding Path=WorkingHours}" Width="30"/>
    </ListView.Items>
</ListView>

So my question is, as soon as I pass the date to my controller and he gets the needed information from the database whats the way to pass the List to my user controls. Do I need to create an Interface which is implemented by the UserControls?

I used the MVVM pattern a lot. But for this university thing we have to use mvc. A little advice would be great.

cheers

1

There are 1 answers

4
goobering On
<ListView x:Name="ListViewEmployees" ItemsSource="{Binding Employees}">
    <ListView.Items>
        <Label ...

You've bound the ItemsSource of the ListView to a collection of Employee objects, then immediately attempted to fill it with Label objects. That's not going to work - if your ItemsSource is bound to a collection all you have to do is fill that collection in your VM/controller, not manually specify XAML items. The closest approximation to what I think you're trying to do is to use a DataTemplate to display the properties of each Employee object:

<ListView x:Name="ListViewEmployees" ItemsSource="{Binding Employees}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>

                <Label Grid.Column="0" x:Name="EmployeeId" Content="{Binding Path=EmployeeId}" Visibility="Collapsed" />
                <Label Grid.Column="1" x:Name="ShortForm" Content="{Binding Path=ShortForm}" Width="40"/>
                <Label Grid.Column="2" x:Name="Degree" Content="{Binding Path=Degree}" Width="90"/>
                <Label Grid.Column="3" x:Name="WorkingHours" Content="{Binding Path=WorkingHours}" Width="30"/>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

For my test case I created a garbage class called Employee with property names matching yours, then created an ObservableCollection, then filled it with test values. The binding took care of the rest.

/// <summary>
/// The <see cref="Employees" /> property's name.
/// </summary>
public const string EmployeesPropertyName = "Employees";

private ObservableCollection<Employee> _employees = null;

/// <summary>
/// Sets and gets the Employees property.
/// Changes to that property's value raise the PropertyChanged event. 
/// </summary>
public ObservableCollection<Employee> Employees
{
    get
    {
        return _employees;
    }

    set
    {
        if (_employees == value)
        {
            return;
        }

        _employees = value;
        RaisePropertyChanged(EmployeesPropertyName);
    }
}


/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
    Employees = new ObservableCollection<Employee>();

    Employees.Add(new Employee { EmployeeId = 1, Degree = "TestDegree1", ShortForm = "TestShortForm1", WorkingHours = 8 });
    Employees.Add(new Employee { EmployeeId = 2, Degree = "TestDegree2", ShortForm = "TestShortForm2", WorkingHours = 7 });
    Employees.Add(new Employee { EmployeeId = 3, Degree = "TestDegree3", ShortForm = "TestShortForm3", WorkingHours = 6 });
    Employees.Add(new Employee { EmployeeId = 4, Degree = "TestDegree4", ShortForm = "TestShortForm4", WorkingHours = 5 });
    Employees.Add(new Employee { EmployeeId = 5, Degree = "TestDegree5", ShortForm = "TestShortForm5", WorkingHours = 4 });
}