当前位置: 首页 > 编程语言 > C#

c#代码隐藏中的数据模板共享

时间:2023-04-10 13:58:38 C#

c#代码隐藏中的数据模板我搜索了一个选项来构建一个带有c#代码的数据模板。我用过:DataTemplatedt=newDataTemplate(typeof(TextBox));绑定bind=newBinding();bind.Path=newPropertyPath("文本");bind.Mode=BindingMode.TwoWay;FrameworkElementFactorytxtElement=newFrameworkElementFactory(typeof(TextBox));txtElement.SetBinding(TextBox.TextProperty,绑定);txtElement.SetValue(TextBox.TextProperty,"测试");dt.VisualTree=txtElement;textBox1.Resources.Add(dt,null);但它不起作用(它被放置在窗口的加载方法中-所以我的文本框应该在窗口的开头显示单词“test”)。有任何想法吗?每个元素都需要添加到当前可视化树中。例如:以上就是C#学习教程:C#代码隐藏中的数据模板分享的全部内容。如果对大家有用,需要了解更多C#学习教程,希望大家多加关注——ListViewparentElement;//例如一个ListView//首先:创建数据模板并将其添加到父控件DataTemplatedt=newDataTemplate(typeof(TextBox));parentElement.ItemTemplate=dt;//第二:创建文本框并将其添加到数据模板FrameworkElementFactorytxtElement=newFrameworkElementFactory(typeof(TextBox));dt.VisualTree=txtElement;//创建绑定Bindingbind=newBinding();bind.Path=newPropertyPath("文本");bind.Mode=BindingMode.TwoWay;//第三:在文本框中设置绑定txtElement.SetBinding(TextBox.TextProperty,bind);txtElement.SetValue(TextBox.TextProperty,"测试");管理员删除。如需转载请注明出处: