简体   繁体   中英

the type DataGrid doesn't support direct Content

I am xaml and c# beginner i am across a situation in silverlight application that i got this error i am trying to create columns using xaml (i dont have to use c# or anything dynamic).

here is my code:

<UserControl x:Class="DEV_CENTER.ProgramGrid"
    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"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
    xmlns:vm="clr-namespace:ViewModel;assembly=ViewModel"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <vm:ProgramViewModel x:Key="ProgramViewModel"/>
    </UserControl.Resources>
    <data:DataGrid x:Name="gridPrograms"
        Grid.Row="0"
        AutoGenerateColumns="False"
        ItemsSource="{Binding Path=ListPrograms }"
        IsReadOnly="True"
        DataContext="{StaticResource ProgramViewModel}">
        <data:DataGridTextColumn Header="c1"
            Binding="{Binding Version}"
            Width="2*"/>
        <data:DataGridTextColumn Header="c2"
            Binding="{Binding Live}"
            Width="2*"/>
        <data:DataGridTextColumn Header="c3"
            Binding="{Binding DateCreation}"
            Width="2*"/>
        <data:DataGridTextColumn Header="..." Width="*"/>
        </data:DataGrid.Columns>
    </data:DataGrid>
</UserControl>

And i get error : the type DataGrid doesn't support direct Content

EDIT:The problem was due to missing <data:DataGrid.Columns> that is solved but second problem is how to bind the column ? Could someone please give me the solution ? I mean i have to create columns using xaml only (not c#) and bind them. How to do that ? thanks

The message means you are not allowed to put anything between the opening and closing tags of the DataGrid as long as you are not "addressing" a DataGrid property, you are missing the opening tag <data:DataGrid.Columns> :

<data:DataGrid ...>
    <data:DataGrid.Columns>
       ...
    </data:DataGrid.Columns>
</data:DataGrid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM