在另一个控件上更改所选项时,WPF MVVM组合框不会填充

时间:2015-12-14 12:44:27

标签: c# wpf mvvm combobox prism

在使用组合框时,我似乎经常遇到这个问题,所以我的问题将包含两部分,首先我做错了什么,为什么?我使用Prism有很多原因,其中一个原因是我不必输入INotifyPropertyChangedDataGrid类。

我希望视图左侧有一个记录列表,以便最终用户轻松选择要查看的记录。在这种情况下,它是<DataGrid Grid.Column="0" Grid.Row="3" AutoGenerateColumns="False" Grid.RowSpan="4" IsReadOnly="True" SelectedItem="{Binding SelectedSweepModel, Mode=TwoWay}" ItemsSource="{Binding DatesList}"> <DataGrid.Columns> <DataGridTextColumn Header="Street" Binding="{Binding Street.StreetName}" /> <DataGridTextColumn Header="Date" Binding="{Binding DateSwept}" /> <DataGridTextColumn Header="Last Name" Binding="{Binding Employee.LastName}" /> <DataGridTextColumn Header="Last Name" Binding="{Binding Employee.FirstName}" /> </DataGrid.Columns> </DataGrid>

ItemSource

它被DatesList List<Model>绑定为SetProperty(请注意INotifyPropertyChanged是Prism的SelectedSweepModel }) private List<SweeperModel> _datesList; public List<SweeperModel> DatesList { get { return _datesList; } set { SetProperty(ref(_datesList),value); } } private SweeperModel _selectedSweepModel; public SweeperModel SelectedSweepModel { get { return _selectedSweepModel; } set { SetProperty(ref (_selectedSweepModel), value); } }

public class SweeperModel
{
    public Streets Street { get; set; }
    public Streets IntersectingStreet1 { get; set; }
    public Streets IntersectionStreet2 { get; set; }
    public EmployeeModel Employee { get; set; }
    public string Quad { get; set; }
    public string Block { get; set; }
    public bool Am { get; set; }
    public bool Pm { get; set; }
    public DateTime DateSwept { get; set; }
    public string UnitsRange { get; set; }
}

SweeperModel:

SweeperModel

comboBox绑定到名为Street的{​​{1}}中的属性:

<ComboBox Grid.Column="2"
          Grid.Row="1"
          ItemsSource="{Binding StreetList}"
          SelectedItem="{Binding SelectedSweepModel.Street}"
          DisplayMemberPath="StreetName" />

Streets类有2个属性:

public class Streets
{
    public string StreetName { get; set; }
    public int StreetID { get; set; }
}

    private List<Streets> _streetList;
    public List<Streets> StreetList
    {
        get { return _streetList; }
        set { SetProperty(ref (_streetList), value); }
    }


当我选择Item时,组合框中没有任何内容。现在我的问题是,我做错了什么?

填充列表我运行一个存储过程,返回一个包含3个表的数据集。

和填充列表的3种方法:

private void FillStreetList(DataTable dt)
{
    var myList = (from DataRow dataRow in dt.Rows
        select new Streets()
        {
            StreetID = dataRow.Field<int>("StreetID"),
            StreetName = dataRow.Field<string>("Streets")
        }).ToList();
    StreetList = myList;
}

private void FillEmployeeList(DataTable dt)
{
    var myList = (from DataRow dataRow in dt.Rows
                  select new EmployeeModel()
                  {
                      Id = dataRow.Field<int>("EmployeeID")
                      ,
                      FirstName = dataRow.Field<string>("FirstName")
                      ,
                      LastName = dataRow.Field<string>("LastName")
                      ,
                      Phone = dataRow.Field<string>("Phone")
                  }).ToList();

    EmployeeList = myList;
}

private void FillJobs(DataTable dt)
{
    var myList = (from DataRow dataRow in dt.Rows
                  orderby dataRow.Field<DateTime>("DateSwept"),
                          dataRow.Field<string>("LastName"),
                          dataRow.Field<string>("FirstName")
                    select new SweeperModel()
                    {
                        Am = dataRow.Field<bool>("TimeOfDayAm"),
                        Pm = dataRow.Field<bool>("TimeOfDayPm"),
                        Block = dataRow.Field<string>("Block"),
                        DateSwept = dataRow.Field<DateTime>("DateSwept"),
                        Employee = new EmployeeModel()
                        {
                            FirstName = dataRow.Field<string>("FirstName"),
                            LastName = dataRow.Field<string>("LastName"),
                            Id = dataRow.Field<int>("EmployeeID"),
                            Phone = dataRow.Field<string>("Phone")
                        },
                        IntersectingStreet1 = new Streets() {StreetName = dataRow.Field<string>("IntersectingStreet1Name"),StreetID = dataRow.Field<int>("IntersectingStreet1") },
                        UnitsRange = dataRow.Field<string>("UnitsRange"),
                        Quad = dataRow.Field<string>("Quad"),
                        Street = new Streets() { StreetName = dataRow.Field<string>("StreetName"), StreetID = dataRow.Field<int>("StreetID") }

                    }).ToList();
    DatesList = myList;

}

1 个答案:

答案 0 :(得分:1)

您对选定的Street对象有不同的引用。一个引用来自StreetList,另一个来自SelectedSweeperModel.Street(SelectedItem)。所以组合框试图通过引用找到所选对象并失败。绑定到StreetID应解决问题

<ComboBox Grid.Column="2"
      Grid.Row="1"
      ItemsSource="{Binding StreetList}"
      SelectedValue="{Binding SelectedSweepModel.Street.StreetID}"
      SelectedValuePath="StreetID"
      DisplayMemberPath="StreetName" />