MVVM绑定无法正常工作

时间:2012-01-02 13:24:13

标签: c# mvvm

更新 管理以修复selectedIndex问题。我忘记设置SelectedItem也自然而然地引起了一些问题。

所以今天上午9点我们完成了24小时的任务,我撞到了一堵砖墙。 我们应该创建一个程序,允许主管添加和删除员工,并添加工作会话,总小时数和总收入。但是我在MVVM-Pattern之后成功地实现了这个问题。出于某种原因,我的Bindings根本无法正常工作,我能看到的唯一解决方案是有人查看我的项目并帮助我排除故障。

这是我的代码 - 我很遗憾不得不发布整个事情,但鉴于我不知道问题在哪里我没有看到任何其他选项。 :

EmployeeModel

[Serializable]
public class WorkSessions : ObservableCollection<WorkSessionModel>
{
    public WorkSessions()
    {

    }
}
[Serializable]
public class WorkSessionModel : INotifyPropertyChanged
{
    private DateTime _dateTime;
    private string _id;
    private double _hours;

    public WorkSessionModel()
    {

    }

    public DateTime DateTime
    {
        get { return _dateTime; }
        set
        {
            _dateTime = value;
            NotifyPropertyChanged("DateTime");
        }
    }

    public string ID
    {
        get { return _id; }
        set
        {
            _id = value;
            NotifyPropertyChanged("ID");
        }
    }
    public double Hours
    {
        get { return _hours; }
        set
        {
            _hours = value;
            NotifyPropertyChanged("Hours");
            NotifyPropertyChanged("TotalHours");
        }
    }


    [field: NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

WorkSessionModel

    [Serializable]
public class WorkSessions : ObservableCollection<WorkSessionModel>
{
    public WorkSessions()
    {

    }
}
[Serializable]
public class WorkSessionModel : INotifyPropertyChanged
{
    private DateTime _dateTime;
    private string _id;
    private double _hours;

    public WorkSessionModel()
    {

    }

    public DateTime DateTime
    {
        get { return _dateTime; }
        set
        {
            _dateTime = value;
            NotifyPropertyChanged("DateTime");
        }
    }

    public string ID
    {
        get { return _id; }
        set
        {
            _id = value;
            NotifyPropertyChanged("ID");
        }
    }
    public double Hours
    {
        get { return _hours; }
        set
        {
            _hours = value;
            NotifyPropertyChanged("Hours");
            NotifyPropertyChanged("TotalHours");
        }
    }


    [field: NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

EmployeeViewModel

public class EmployeeViewModel : ViewModelBase
{
    private Employees _employeesModel = new Employees();
    public Employees EmployeesView = new Employees();

    public ObservableCollection<WorkSessionModel> WorkSessions { get; set; }

    private string _id = "0";
    private string _name = "noname";
    private double _wage = 0;
    private int _totalhours = 0;
    public string ID
    {
        get { return _id; }
        set { _id = value; RaisePropertyChanged("ID"); }
    }

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            RaisePropertyChanged("Name");
        }
    }
    public double Wage
    {
        get { return _wage; }
        set
        {
            _wage = value;
            RaisePropertyChanged("Wage");
        }
    }
    public int TotalHours
    {
        get { return _totalhours; }
        set
        {
            _totalhours = value;
            RaisePropertyChanged("TotalHours");
        }
    }

    private EmployeeModel _selectedEmployee = new EmployeeModel();
    public EmployeeModel SelectedEmployee
    {
        get { return _selectedEmployee; }
        set
        {
            _selectedEmployee = value;
            RaisePropertyChanged("SelectedEmployee");
        }
    }

    private int _selectedEmployeeIndex;
    public int SelectedEmployeeIndex
    {
        get { return _selectedEmployeeIndex; }
        set
        {
            _selectedEmployeeIndex = value;
            RaisePropertyChanged("SelectedEmployeeIndex");
        }
    }

    #region RelayCommands

    // Employee Relay Commands
    public RelayCommand EmployeeAddNewCommand { set; get; }
    public RelayCommand EmployeeDeleteCommand { set; get; }
    public RelayCommand EmployeeNextCommand { set; get; }
    public RelayCommand EmployeePrevCommand { set; get; }
    public RelayCommand EmployeeTotalHoursCommand { get; set; }

    #endregion

    public EmployeeViewModel()
    {
        InitCommands();
    }

    private void InitCommands()
    {           
        EmployeeAddNewCommand = new RelayCommand(EmployeeAddNewExecute, EmployeeAddNewCanExecute);
        EmployeeDeleteCommand = new RelayCommand(EmployeeDeleteNewExecute, EmployeeDeleteCanExecute);
        EmployeeNextCommand = new RelayCommand(EmployeeNextExecute, EmployeeNextCanExecute);
        EmployeePrevCommand = new RelayCommand(EmployeePrevExecute, EmployeePrevCanExecute);
        //EmployeeTotalHoursCommand = new RelayCommand(EmployeeTotalHoursExecute, EmployeeTotalHoursCanExecute);
    }

    //private void EmployeeTotalHoursExecute()
    //{
    //    _selectedEmployee.TotalHours();
    //}

    //private bool EmployeeTotalHoursCanExecute()
    //{
    //    return true;
    //}

    private void EmployeeAddNewExecute()
    {
        EmployeeModel newEmployee = new EmployeeModel();
        EmployeesView.Add(newEmployee);
        _employeesModel.Add(newEmployee);
        SelectedEmployee = newEmployee;
    }

    private bool EmployeeAddNewCanExecute()
    {
        return true;
    }

    private void EmployeeDeleteNewExecute()
    {
        if (MessageBox.Show("You are about delete all submissions for     Employee," + SelectedEmployee.Name + "(" + SelectedEmployee.ID +")\r\nAre you sure?", "This is a Warning!", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
        {
            _employeesModel.Remove(SelectedEmployee);
            EmployeesView.Remove(SelectedEmployee);
        }
    }

    private bool EmployeeDeleteCanExecute()
    {
        if (SelectedEmployee != null)
            return true;
        else return false;
    }

    private void EmployeeNextExecute()
    {
        SelectedEmployeeIndex++;
    }
    private bool EmployeeNextCanExecute()
    {
        if (SelectedEmployeeIndex < EmployeesView.Count - 1)
            return true;
        return false;
    }

    private void EmployeePrevExecute()
    {
        SelectedEmployeeIndex--;
    }
    private bool EmployeePrevCanExecute()
    {
        if (SelectedEmployeeIndex > 0)
            return true;
        return false;
    }
}

查看

    public partial class MainWindow : Window
{
    public EmployeeViewModel EmployeeViewModel = new EmployeeViewModel();

    public MainWindow()
    {
        InitializeComponent();

        menu_employee.DataContext = EmployeeViewModel;
        sp_employees.DataContext = EmployeeViewModel;
        datagrid_employees.ItemsSource = EmployeeViewModel.EmployeesView;
        grid_selectedEmployee.DataContext =  EmployeeViewModel.SelectedEmployee;
    }
}

2 个答案:

答案 0 :(得分:0)

我可以看到您的代码存在一些问题:

  • 更新SelectedIndex时,SelectedItem保持不变,反之亦然。
  • 您的数据绑定似乎无序:

DataContext属性级联到某个依赖项对象的每个子级。

MainWindow构造函数中的代码应该替换为:

this.DataContext = EmployeeViewModel;

然后在XAML中使用数据绑定设置其余属性。您遇到的问题是,selectedemployee的DataContext只设置一次。这意味着如果您选择其他员工,则不会更新。

SelectedEmployee网格的示例:

<Grid Name="grid_selectedEmployee" DataContext="{Binding SelectedEmployee, 
UpdateSourceTrigger=PropertyChanged}">...</Grid>

答案 1 :(得分:0)

我看到的最重要的事情之一是你设置属性,而不是绑定

例如,

datagrid_employees.ItemsSource = EmployeeViewModel.EmployeesView;

您告诉您的DataGrid,ItemsSource应 该特定对象。您需要将其绑定到该值,以便您告诉它指向该属性。这将使您的UI正确反映ViewModel中的内容

我看到的另一个巨大的红旗是你的ViewModel引用了一个名为EmployeeView的东西,这让我相信你的View和ViewModel太紧密地联系在一起了。 您的ViewModel应包含所有业务逻辑和代码,而View通常是XAML,只是以用户友好的方式反映ViewModel。

View和ViewModel永远不应该直接相互引用(在某些极少数情况下我的View引用了我的ViewModel,但从来没有相反)

例如,EmployeesViewModel可能包含

  • ObservableCollection<Employee> Employees
  • Employee SelectedEmployee
  • ICommand AddEmployeeCommand
  • ICommand DeleteEmployeeCommand

而您的View(XAML)可能如下所示:

<StackPanel>
    <StackPanel Orientation="Horizontal">
        <Button Content="Add" Command="{Binding AddEmployeeCommand}" />
        <Button Content="Delete" Command="{Binding DeleteEmployeeCommand}" />
    </StackPanel>

    <DataGrid ItemsSource="{Binding Employees}"
              SelectedItem="{Binding SelectedEmployee}">
        ... etc
    </DataGrid>

    <UniformGrid DataContext="{Binding SelectedEmployee}" Columns="2" Rows="4">
        <TextBlock Text="ID" />
        <TextBox Text="{Binding Id}" />
        ... etc
    </UniformGrid >
</StackPanel>

您应该设置的唯一内容是整个Window的DataContext。通常我会覆盖App.OnStartup()来启动我的应用程序:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        var view = new MainWindow();
        var vm = new EmployeesViewModel;
        view.DataContext = vm;
        view.Show();
    }
}

虽然我认为在你的情况下这也可行:

public MainWindow()
{
    InitializeComponent();

    this.DataContext =  new EmployeesViewModel();
}