Click here to Skip to main content
15,895,423 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a custom class called TextBoxColumn as Follows
C#
public class TextBoxColumn : DataGridTemplateColumn
{
     public static readonly DependencyProperty FieldNameProperty = DependencyProperty.Register("FieldName", typeof(string), typeof(TextBoxColumn), new PropertyMetadata(""));
        public string FieldName
        {
            get { return (string)GetValue(FieldNameProperty); }
            set { SetValue(FieldNameProperty, value); }
        }
     ...
}

When creating DataGrid columns from XAML:
XML
<DataGrid>
    <DataGrid.Columns>
        <local:TextBoxControl FieldName="FirstName"/>
        <local:TextBoxControl FieldName="LastName"/>
    </DataGrid.Columns>
</DataGrid>

In XAML Dictionary, I have defined the Cell Template for this TextBoxColumn:
HTML
<DataTemplate x:Key="TextBoxColumn_CellTemplate">
    <TextBox Text="{Binding FieldName}"/> <!-- Here is the problem -->
</DataTemplate>

How to get the value of FieldName property of TextBoxColumn and Bind it to Text property? How can I achieve it without C# code?
Posted

1 solution

hiii,
just place this code
TextBoxColumn obj=new TextBoxColumn()
textbox1.DataContext=obj;

or read this article will help you
 
Share this answer
 
v2
Comments
Debashis Pal 5-Sep-12 2:39am    
Thanks Ganesh. But seems the article link is missing. However I am setting the DataContext. The problem is, if I use binding inside data template as:
Text="{Binding FirstName}"
This works fine. But want it to be dynamic and reusable, so trying to make it as:
Text="{Binding FieldName}"
The binding should be with the value of FieldName property (e.g. FirstName, LastName etc as Mentioning while creating the control. How to get value of FieldName and "bind" with the "value" of FieldName property. Please help.

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