使用DataTemplate [英] work with DataTemplate

查看:80
本文介绍了使用DataTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在WPF应用程序的DataTemplte中使用控件时遇到问题,问题是如何在不使用xaml代码中绑定的情况下从代码中更改TextBox文本,我确实编写了此函数,但未显示任何内容:

Hi guys,

I have problem working with control in DataTemplte in WPF applications, the problem is how I can change the TextBox text from code behind without using the binding in xaml code, I did write this function but nothing appear:

private void SetTextBoxData(string textBoxName,string value)
{
FrameWorkElement fw;

st=dtEng.LoadContent() as FrameworkElement; // dtEng is the name of DataTemplate

(st.FindName(textBoxName) as TextBox).Text=value;

} 


这是我设置位于dtEng(DataTemplate)中的文本框文本的功能,但是没有任何动作,没有结果.

谁能帮我解决这个问题,并认为您非常有帮助.


This is my function to set the textbox text located in dtEng(DataTemplate), but no action no result appear.

Can any one help me with this problem, and thinks you very mach.

推荐答案

您将不得不在这里使用数据绑定.您应该在后面的代码中创建一个公共字符串属性,并将数据模板文本框的Text属性绑定到该属性.
You are going to have to use Data Binding here. You should create a public string property in your code behind and bind the Text property of your data template textbox to that property.
<DataTemplate>
            <TextBox Text="{Binding Path=Name}"/>
        </DataTemplate>





string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (name != value)
                {
                    name = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        void SetTextBoxText(string value)
        {
            Name = value;
        }


您需要确保您的表单实现INotifyPropertyChanged,并且表单datacontext设置为此


You need to ensure that your form implements INotifyPropertyChanged and that the forms datacontext is set to this

this.DataContext = this;



希望对您有帮助



Hope this helps


这篇关于使用DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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