2011-03-07 51 views
0

我有一個數據網格,它綁定到一個RIA數據對象。此數據網格是通過從Visual Studio中的數據源選項卡中拖放表創建的。數據網格工作正常。DataGrid中的組合框不會顯示數據

我添加了一個額外的列,我已經通過DataGridTemplateColumn添加了一個Combobox。這個組合框綁定到一個domaindatasource。但是,組合框不包含任何數據。

我從XAML獲取了組合框代碼,並將其作爲單獨的數據網格控件粘貼,並且組合框工作正常並顯示數據。

爲什麼我的組合框包含在數據網格中時不顯示任何數據?我的XAML代碼如下:

<navigation:Page x:Class="Chemical_Management_System.EditPermissions" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d" 
     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
     d:DesignWidth="640" d:DesignHeight="480" 
     Title="EditPermissions Page" Width="640" HorizontalAlignment="Center" xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices" xmlns:my="clr-namespace:Chemical_Management_System.Web" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> 
<Grid x:Name="LayoutRoot"> 
    <Border BorderBrush="Silver" BorderThickness="1" Height="45" HorizontalAlignment="Left" Margin="10,10,0,0" Name="Border1" Style="{StaticResource HeaderBackground}" VerticalAlignment="Top" Width="618"> 
     <TextBlock Height="23" Name="TextBlock21" Style="{StaticResource HeaderText}" Text="Edit Permissions" /> 
    </Border> 
    <riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:Permissions, CreateList=true}" Height="0" Name="PermissionsDomainDataSource" QueryName="GetPermissionsQuery" Width="0"> 
     <riaControls:DomainDataSource.DomainContext> 
      <my:DataContext /> 
     </riaControls:DomainDataSource.DomainContext> 
    </riaControls:DomainDataSource> 

    <riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:PermissionLookups, CreateList=true}" Height="0" Name="PermissionLookupsDomainDataSource" QueryName="GetPermissionLookupsQuery" Width="0"> 
     <riaControls:DomainDataSource.DomainContext> 
      <my:DataContext /> 
     </riaControls:DomainDataSource.DomainContext> 
    </riaControls:DomainDataSource> 

    <sdk:DataGrid AutoGenerateColumns="False" Height="366" HorizontalAlignment="Left" ItemsSource="{Binding ElementName=PermissionsDomainDataSource, Path=Data}" Margin="12,61,0,0" Name="PermissionsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="615"> 
     <sdk:DataGrid.Columns> 
      <sdk:DataGridCheckBoxColumn x:Name="DeputyColumn" Binding="{Binding Path=Deputy}" Header="Deputy" Width="SizeToHeader" /> 
      <sdk:DataGridTextColumn x:Name="DescriptionColumn" Binding="{Binding Path=Description}" Header="Description" Width="SizeToHeader" /> 
      <sdk:DataGridTextColumn x:Name="IDColumn" Binding="{Binding Path=ID, Mode=OneWay}" Header="ID" IsReadOnly="True" Width="SizeToHeader" /> 
      <sdk:DataGridTextColumn x:Name="PermissionIDColumn" Binding="{Binding Path=PermissionID}" Header="Permission ID" Width="SizeToHeader" /> 
      <sdk:DataGridCheckBoxColumn x:Name="PrimaryColumn" Binding="{Binding Path=Primary}" Header="Primary" Width="SizeToHeader" /> 
      <sdk:DataGridTextColumn x:Name="UserIDColumn" Binding="{Binding Path=User.Name}" Header="User" Width="SizeToHeader" /> 


      <sdk:DataGridTemplateColumn Header="Permission"> 

       <sdk:DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox Height="23" HorizontalAlignment="Left" Name="ComboBox1" VerticalAlignment="Top" Width="120" ItemsSource="{Binding Data, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, TargetNullValue='',ElementName=PermissionLookupsDomainDataSource}" > 
          <ComboBox.ItemTemplate> 
           <DataTemplate> 
            <TextBlock Text="{Binding Description}" /> 
           </DataTemplate> 
          </ComboBox.ItemTemplate> 
         </ComboBox> 
        </DataTemplate> 
       </sdk:DataGridTemplateColumn.CellTemplate> 
      </sdk:DataGridTemplateColumn> 


     </sdk:DataGrid.Columns> 
    </sdk:DataGrid> 

    <ComboBox Height="23" HorizontalAlignment="Left" Margin="194,446,0,0" Name="ComboBox1" VerticalAlignment="Top" Width="120" ItemsSource="{Binding Data, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, TargetNullValue='',ElementName=PermissionLookupsDomainDataSource}" > 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Description}" /> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 
</Grid> 

回答

1

我相信這個問題正在發生,因爲一個DataTemplate住在自己的DataContext,並且不能夠訪問父元素,包括您的DomainDataSource。這是我遇到的一個相當令人沮喪的問題。

這些鏈接幫我指出了正確的方向:

SO Question

Creating a Silverlight DataContext Proxy to Simplify Data Binding in Nested Controls

如果你不反對使用後面的代碼,我相信在加載設置的ItemSource的stackoverflow solution事件將是最簡單的修復。

+0

謝謝,那曾經工作過!雖然這似乎是MS的一點疏忽...... – 2011-03-08 09:05:01