Combobox within datagrid wont show data

1.7k views Asked by At

I have a datagrid, which is bound to a RIA data object. this datagrid was created by dragging and dropping the table from the datasources tab within Visual Studio. the datagrid works fine.

I have added an extra column, on which I have added a Combobox via a DataGridTemplateColumn. this combobox is bound to a domaindatasource. However the combobox does not contain any data.

I took the combobox code from XAML, and pasted it as a seperate control outwith the datagrid, and the combobox works fine and displays data.

Why does my combobox not display any data when included within a datagrid? My XAML code is as follows:

<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

There are 1 answers

1
grimus On BEST ANSWER

I believe the problem is occurring because a DataTemplate lives in its own DataContext, and isn't able to access parent elements, including your DomainDataSource. Its quite a frustrating problem that I've run into as well.

These links helped point me in the right direction:

SO Question

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

If you're not opposed to using code behind, I believe the stackoverflow solution of setting the ItemSource on the Loaded event will be the easiest fix.