用于两个UserControl的MVC WPF DataContext

时间:2015-06-07 16:20:43

标签: wpf model-view-controller data-binding user-controls datacontext

我正在创建一个WPF应用程序。

我有一个View,它实现了两个不同的User-Controls。 每个控件的数据上下文需要相同,因为当我在一个控件中更改值时,它应该影响另一个控件。 只要在视图中选择日期,就应该为每个控件设置DataContext(这是一个信息列表)。 我已经尝试将List绑定到Control。什么样的工作。列表框显示有元素,但内容不可见。

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

所以我的问题是,只要我将日期传递给我的控制器,他就会从数据库中获取所需的信息,以便将List传递给我的用户控件。我是否需要创建一个由UserControls实现的接口?

我经常使用MVVM模式。但对于这所大学的事情,我们必须使用mvc。 一点建议会很棒。

欢呼声

1 个答案:

答案 0 :(得分:0)

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

您已将ListView的ItemsSource绑定到Employee对象的集合,然后立即尝试使用Label对象填充它。这不起作用 - 如果您的ItemsSource绑定到集合,您只需在VM /控制器中填充该集合,手动指定XAML项目。最接近我认为您尝试做的事情是使用DataTemplate显示每个Employee对象的属性:

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

对于我的测试用例,我创建了一个名为Employee的垃圾类,其属性名称与您的属性相匹配,然后创建了一个ObservableCollection,然后用测试值填充它。绑定照顾其余部分。

/// <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 });
}