2010-07-07 82 views
0

我想要使用ViewModel聲明式綁定DataGrid CellEditingTemplate中的ComboBox。 ComboBox沒有被綁定。我究竟做錯了什麼?Silverlight MVVM - 如何使用ViewModel聲明式綁定DataGrid CellEditingTemplate中的組合框?

XAML:

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" 
    xmlns:data="clr-namespace:SilverlightApplication1" 
    mc:Ignorable="d" 
    x:Class="SilverlightApplication1.EmployeeDetail" 
    Width="640" Height="480"> 

    <UserControl.Resources> 
     <data:EmployeeDetailsViewModel 
      x:Key="ViewModel" 
      d:IsDataSource="True" /> 
    </UserControl.Resources> 

    <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource ViewModel}}" Background="White"> 

     <sdk:DataGrid ItemsSource="{Binding Employees,Mode=TwoWay}" AutoGenerateColumns="False" CanUserSortColumns="True" CanUserReorderColumns="True" CanUserResizeColumns="True" GridLinesVisibility="All" Height="317" HorizontalAlignment="Left" Margin="12,136,0,0" Name="EmployeesGrid" VerticalAlignment="Top" Width="605"> 
      <sdk:DataGrid.Columns> 


<!-- snipped from brevity --> 

       <sdk:DataGridTemplateColumn Header="Status"> 
        <sdk:DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <TextBlock Text="{Binding EmployeeStatus.Description}" TextWrapping="Wrap"></TextBlock> 
         </DataTemplate> 
        </sdk:DataGridTemplateColumn.CellTemplate> 
        <sdk:DataGridTemplateColumn.CellEditingTemplate> 
         <DataTemplate> 
          <ComboBox ItemsSource="{Binding Path=EmployeeStatuses}" SelectedItem="{Binding EmployeeStatus, Mode=TwoWay}" /> 
         </DataTemplate> 
        </sdk:DataGridTemplateColumn.CellEditingTemplate> 
       </sdk:DataGridTemplateColumn> 

      </sdk:DataGrid.Columns> 
     </sdk:DataGrid> 
     <TextBlock x:Name="SearchLabel" HorizontalAlignment="Left" Margin="12,95,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="106" Height="34"><Run FontWeight="Bold" Text="Search By Name: "/><Run FontSize="9.333" Text="(Last, First)"/></TextBlock> 
     <TextBox x:Name="SearchParam" HorizontalAlignment="Left" Margin="144,101,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="162"/> 
     <Button x:Name="SearchButton" Content="Search" HorizontalAlignment="Right" Margin="0,102,242,0" VerticalAlignment="Top" Width="75" Click="SearchButton_Click"/>  


    </Grid> 
</UserControl> 

視圖模型:

using System.Collections.ObjectModel; 
using SilverlightApplication1.EmployeeService; 
using SilverlightApplication1.ViewModels; 

namespace SilverlightApplication1 
{ 
    public class EmployeeDetailsViewModel : ViewModelBase 
    { 
     readonly IEmployeeServiceAgent _serviceAgent; 
     ObservableCollection<EmployeeStatus> _employeeStatuses { get; set; } 
     ObservableCollection<Employee> _employees { get; set; } 

     public EmployeeDetailsViewModel() : this(new EmployeeServiceAgent()) { } 
     public EmployeeDetailsViewModel(IEmployeeServiceAgent serviceAgent) 
     { 
      if (!IsDesignTime) 
      { 
       _serviceAgent = serviceAgent; 
       GetAllEmployees(); 
       GetEmployeeStatuses(); 
      } 

     } 

     public ObservableCollection<Employee> Employees 
     { 
      get { return _employees; } 
      set 
      { 
       if(_employees!=value) 
       { 
        _employees = value; 
        OnNotifyPropertyChanged("Employees"); 
       } 
      } 
     } 

     public ObservableCollection<EmployeeStatus> EmployeeStatuses 
     { 
      get { return _employeeStatuses; } 
      set 
      { 
       if (_employeeStatuses != value) 
       { 
        _employeeStatuses = value; 
        OnNotifyPropertyChanged("EmployeeStatuses"); 
       } 
      } 
     } 

     private void GetAllEmployees() 
     { 
      _serviceAgent.GetAll((s, e) => Employees = e.Result); 
     } 

     private void GetEmployeeStatuses() 
     { 
      _serviceAgent.GetEmployeeStatuses((s, e) => EmployeeStatuses = e.Result); 
     } 

    } 
} 

更新:

這似乎是錯誤的,但我想通了,如何讓通過重新引用視圖模型綁定工作在ItemSource綁定中:

<ComboBox ItemsSource="{Binding Source={StaticResource ViewModel},Path=EmployeeStatuses}" 
             DisplayMemberPath="Description" 
             SelectedItem="{Binding EmployeeStatus, Mode=TwoWay}" /> 

但是,我現在遇到SelectedItem未綁定的問題!我究竟做錯了什麼?

回答

2

這個問題是人們碰到的常見問題。當您在列的數據模板中時,您不再限制視圖模型。此時您的數據上下文是EmployeeStatus對象(它沒有要綁定的EmployeeStatuses屬性)。

因此,要使組合框工作,可以使用ElementName = LayoutRoot將樹備份到根ViewModel。

更新:這裏將是你的綁定完整的語法:

{結合DataContext.EmployeeStatuses,的ElementName = LayoutRoot}

UPDATE2:其實我已經運行到這個問題,以及和有一個workaround you have to implement可以讓元素名稱綁定在數據網格中工作。

+1

您的意思是添加的ElementName = LayoutRoot到ComboBox的的ItemsSource?我這樣做了,但ComboBox仍然沒有綁定。 Rokal 2010-07-07 19:28:11

+1

科比,我試過 Rokal 2010-07-07 20:06:21

+0

嘗試在上次更新的鏈接中使用解決方法。 – Bryant 2010-07-08 14:43:47

相關問題