【问题标题】:Silverlight DataGrid Combobox Newbie QuestionSilverlight DataGrid Combobox 新手问题
【发布时间】:2011-09-25 09:56:36
【问题描述】:

我正在使用 silverlight 中的数据网格。我有一个 WCF 服务,它返回一个在我填充数据网格时可以正常工作的列表。 CoreEmployee 返回 EmployeeId、FirstName、LastName、HourlyRate、HireDate 的属性。这是我的每小时费率的 XAML:

<data:DataGridTemplateColumn Header="Hourly Rate">
    <data:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding HourlyRate}" />
        </DataTemplate>
    </data:DataGridTemplateColumn.CellTemplate>
    <data:DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <!--ItemsSource="{Binding PayRateList, Source={StaticResource PayRateProvider}}"-->
            <ComboBox SelectedItem="{Binding HourlyRate}" 
                      ItemsSource="{Binding HourlyRates}"
                      local:ComboBoxService.ForceOpen="true"
                  />
        </DataTemplate>
    </data:DataGridTemplateColumn.CellEditingTemplate>
</data:DataGridTemplateColumn>

这是我想要完成的任务:当每个员工的小时费率填充到数据网格中时,我还想要数据网格中每个人的所有唯一工资率的列表。

我后面的代码就是这样做的:

private List<Decimal> _hourlyRates = new List<decimal>();
public List<Decimal> HourlyRates
{
    get { return _hourlyRates; }
}

void client_GetEmployeesCompleted(object sender, GetEmployeesCompletedEventArgs e)
{
    try
    {
        if (e.Result != null)
        {
            EmployeesGrid.ItemsSource = e.Result;

            // Convert an ObservableCollection<T> to a List<T>
            List<CoreEmployee> employees = e.Result.ToList<CoreEmployee>();

            // Generate a unique list
            // http://stackoverflow.com/questions/223400/checking-for-duplicates-in-a-complex-object-using-linq-or-lamda-expression
            var results = from item in employees
                          group item by item.HourlyRate into g
                          select g.First();

            foreach (CoreEmployee employee in results)
            {
                HourlyRates.Add(employee.HourlyRate);
            }

            _dataHasLoaded = true;
        }
    }
    catch (Exception exc)
    {
        // Eat the exception
    }
}

但是,当我尝试双击文本块时出现问题,组合框确实显示,但没有任何数据。

我做错了什么?

【问题讨论】:

    标签: silverlight


    【解决方案1】:

    在 foreach 循环中填充列表后,您应该在 HourlyRates 属性上引发 PropertyChanged 事件。另外,将 ComboBox SelectedItem 绑定的 Mode 设置为 TwoWay。

    【讨论】:

      猜你喜欢
      • 2011-11-24
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      • 2011-09-27
      • 2011-06-17
      相关资源
      最近更新 更多