为什么我在WPF网格中看不到任何数据?

时间:2010-10-05 21:46:48

标签: c# wpf data-binding grid datatemplate

    <DataTemplate x:Key="tmpGrdProducts">


        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>

            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Image Grid.RowSpan="6" Width="200" Height="200" Grid.Column="0" Grid.Row="0"></Image>
            <TextBlock Grid.ColumnSpan="2" Grid.Row="0" Grid.Column="1" Text="{Binding ProductName}" Foreground="Red"></TextBlock>

            <!-- Labels-->
            <TextBlock Text="SKU" Foreground="Red" Grid.Column="1" Grid.Row="1"></TextBlock>
            <TextBlock Text="Code" Foreground="Red" Grid.Column="1" Grid.Row="2"></TextBlock>
            <TextBlock Text="Mark" Foreground="Red" Grid.Column="1" Grid.Row="3"></TextBlock>
            <TextBlock Text="Model" Foreground="Red" Grid.Column="1" Grid.Row="4"></TextBlock>

            <!-- Data-->
            <TextBlock Text="{Binding SKU}" Foreground="Black" Grid.Column="1" Grid.Row="1"></TextBlock>
            <TextBlock Text="{Binding ProductCode}" Foreground="Black" Grid.Column="1" Grid.Row="2"></TextBlock>
            <TextBlock Text="{Binding Mark}" Foreground="Black" Grid.Column="1" Grid.Row="3"></TextBlock>
            <TextBlock Text="{Binding Model}" Foreground="Black" Grid.Column="1" Grid.Row="4"></TextBlock>

        </Grid>


    </DataTemplate>



</Page.Resources>


<Grid x:Name="grdProduct" DataContext="Binding">

    <ItemsControl>

        <ItemsControl ItemTemplate="{StaticResource tmpGrdProducts}"></ItemsControl>


    </ItemsControl>




</Grid>

源代码

var Products = from t in bsEntity.ProductsTemps select t;

grdProduct.DataContext = Products;

谢谢。

1 个答案:

答案 0 :(得分:3)

您需要将ItemsSource的{​​{1}}属性绑定到集合。 同样在您的项目控件中,您已使用项目模板声明了额外的项目控件。

您正在寻找的是:

ItemsControl

另外,我认为你的网格上不需要<ItemsControl ItemsSource="{Binding Path=YourCollectionProperty}"> <ItemsControl.ItemTemplate> <!--your data template here--> </ItemsControl.ItemTemplate> </ItemsControl> 。如果有的话,你可能需要DataContext="Binding"