Click here to Skip to main content
15,122,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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;

} 

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.
Posted
Updated 10-Jan-12 0:18am
v2

1 solution

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.
XML
<DataTemplate>
            <TextBox Text="{Binding Path=Name}"/>
        </DataTemplate>


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

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

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

this.DataContext = this;


Hope this helps
   
Comments
The-vister 10-Jan-12 6:06am
   
ok thx i will try it,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900