[WPF]使用C#代码实现DataTemplate

http://www.cnblogs.com/chenlhuaf/archive/2009/02/11/1388426.html

 

 

    GridViewColumn column = new GridViewColumn();
            GridViewColumnHeader h = new GridViewColumnHeader();
            h.Content = "??????";
            h.Tag = "*******";
            column.Header = h;
            DataTemplate dt = new DataTemplate();
            FrameworkElementFactory fef = new FrameworkElementFactory(typeof(CheckBox));
            Binding binding = new Binding();
            binding.Path = new PropertyPath("MarketIndicator");
            fef.SetBinding(CheckBox.ContentProperty, binding);
            fef.SetValue(CheckBox.ForegroundProperty, Brushes.White);
            dt.VisualTree = fef;
            column.CellTemplate = dt;
            (this.lvExecutionTable.View as GridView).Columns.Add(column);

 

 

http://hi.baidu.com/icexile/blog/item/2e75a73d47171108bba167e2.html

 

//内存中动态生成一个XAML,描述了一个DataTemplate
XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XElement xDataTemplate =
new XElement( ns + "DataTemplate", new XAttribute( "xmlns", "http://schemas.microsoft.com/winfx/2006/xaml/presentation" ),
new XElement( ns + "TextBlock",
new XElement( ns + "TextBlock", new XAttribute( "Width", "100" ), new XAttribute( "Text", "{Binding Path=Name}" ) ),
new XElement( ns + "TextBlock", new XAttribute( "Width", "100" ), new XAttribute( "Text", "{Binding Path=Nick}" ) )
)
);
//将内存中的XAML实例化成为DataTemplate对象,并赋值给
//ListBox的ItemTemplate属性,完成数据绑定
XmlReader xr = xDataTemplate.CreateReader();
DataTemplate dataTemplate = XamlReader.Load( xr ) as DataTemplate;
lstBox_.ItemTemplate = dataTemplate;
//将两个TextBox分别绑定到当前Item的Name和Nick属性上
txtBoxName_.SetBinding( TextBox.TextProperty, "Name" );
txtBoxNick_.SetBinding( TextBox.TextProperty, "Nick" );
}

转载于:https://www.cnblogs.com/sera/archive/2011/03/24/1993463.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值