带有datagrid的Combobox不会显示数据

时间:2011-03-07 14:29:13

标签: silverlight xaml datagrid combobox datagridtemplatecolumn

我有一个datagrid,它绑定到一个RIA数据对象。此数据网格是通过从Visual Studio中的数据源选项卡中拖放表来创建的。数据网格工作正常。

我添加了一个额外的列,我通过DataGridTemplateColumn添加了一个Combobox。这个组合框与一个domaindatasource绑定。但是,组合框不包含任何数据。

我从XAML中获取了组合框代码,并将其作为一个单独的控件粘贴到datagrid中,并且组合框工作正常并显示数据。

为什么我的组合框在数据网格中包含时不显示任何数据?我的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 个答案:

答案 0 :(得分:1)

我认为问题正在发生,因为DataTemplate存在于自己的DataContext中,并且无法访问父元素,包括DomainDataSource。这也是我遇到的一个非常令人沮丧的问题。

这些链接帮助我指明了正确的方向:

SO Question

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

如果您不反对使用代码,我相信在Loaded事件上设置ItemSource的stackoverflow solution将是最简单的修复。