如何在代码中定义DataTemplate? [英] How to define a DataTemplate in code?

查看:239
本文介绍了如何在代码中定义DataTemplate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在代码(使用C#)中创建一个 DataTemplate ,然后在 DataTemplate 中添加一个控件?

How can I create a DataTemplate in code (using C#) and then add a control to that DataTemplate?

<data:DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <Border>
            <Border Margin="10" Padding="10" BorderBrush="SteelBlue" 
                 BorderThickness="3" CornerRadius="5">
                <TextBlock Text="{Binding Description}" TextWrapping="Wrap" 
                     FontSize="10">
                </TextBlock>
            </Border>
        </Border>
    </DataTemplate>
</data:DataGrid.RowDetailsTemplate>

我正在使用Sivlerlight。

I am using Sivlerlight.

推荐答案

据我所知,在Silverlight中创建 DataTemplate 的唯一方法是使用 XamlReader 。基本上你只需要传递一个XAML作为一个字符串,它会给你一个 DataTemplate 。 Byron的解决方案将适用于WPF,但Silverlight(据我所知)不支持 FrameworkElementFactory

As far as I know, the only way to create a DataTemplate in Silverlight is to use XamlReader. Basically you would just pass it the XAML as a string and it will give you back a DataTemplate. Byron's solution would apply to WPF but Silverlight (to the best of my knowledge) does not support FrameworkElementFactory.

a href =http://blogs.msdn.com/scmorris/archive/2008/04/14/defining-silverlight-datagrid-columns-at-runtime.aspx =nofollow noreferrer> Scott Morrison:定义Silverlight运行时的DataGrid列

Scott Morrison: Defining Silverlight DataGrid Columns at Runtime

记下 DataGridTemplateColumn 的选项#2。

这篇关于如何在代码中定义DataTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆